The Ultimate Python Tutorial PDF – Learn Python from Basics to Advanced

Here is a 2500+ word blog post on "python tutorial pdf" related to python code examples pdf:

Python is one of the most popular programming languages today due to its easy readability, versatility, and huge community support. Whether you are a beginner or an experienced developer, having a solid grasp of Python can open up opportunities in web development, data analysis, artificial intelligence, and more.

This comprehensive Python tutorial PDF acts as a guide for learning Python from scratch. It covers everything from basic syntax, data structures, conditional statements to advanced concepts like OOPs, regular expressions, file handling etc. with coding examples for practice.

Section 1 – Python Programming Basics

Installing Python

The first step to learning Python is getting it installed on your computer. Python comes pre-installed on most Linux and Mac devices. For Windows, you can download the latest Python release from the official website python.org. We recommend installing Anaconda distribution as it comes bundled with the most common Python packages for data science and AI.

Follow the installer wizard by keeping all default options. Make sure to check the box that says "Add Python to PATH" so you can invoke it from the command line.

Creating Your First Python Program

Open a text editor like Sublime or Notepad++ and save the following code in a file named hello.py. This prints out a simple "Hello World" message and introduces you to the print function.

print("Hello World!")

Open command prompt, navigate to the directory where this file is saved and run:

python hello.py

You should see the hello world message printed! Congratulations on running your first Python program.

Main Function

Unlike other languages, Python doesn‘t use a main function. Code begins executing from the first line itself.

print("This gets executed first")

print("This gets executed next")

All statements are executed sequentially from top to bottom.

Variables in Python

Variables are used to store data in programs. They act as containers for values.

name = "John" # String variable 

age = 20 # Numeric variable

is_adult = True # Boolean variable 

Naming rules for Python variables:

  • Can contain alphabets, digits and underscores
  • Cannot start with digit
  • Case sensitive

Section 2 – Python Data Structures

Some common data structures in Python are lists, tuples, dictionaries. These allow you to store and access data in different ways.

Python Lists

Lists are ordered collection of data which allow duplicates. They are defined with square brackets and data items are separated by comma.

fruits = ["apple", "banana", "cherry"]

fruits[0] # Access first element (apple)

fruits[-1] # Access last element (cherry)

Python Tuples

Tuples are similar to lists but they are immutable which means the data inside a tuple cannot be modified. Tuples use parantheses instead of square brackets.

colors = ("red", "blue", "green")

colors[0] = "yellow" # Gives error as tuples cannot be changed

Python Dictionary

Dictionaries contain key-value pairs for data. They are unordered and defined within curly braces.

user = {
  "name": "Sam",
  "age": 30,
  "verified": True 
}

print(user["name"]) # Get value of key "name"

Section 3 – Python Conditional and Loops

If Statement

If statements allow you to check conditions and execute code only when the condition is true.

age = 25
if age > 18:
   print("You are eligible to vote")

For Loop

For loops let you iterate over a sequence like list, tuple etc and execute code for each item.

for fruit in ["apple", "banana"]:
  print(fruit) 

While Loop

While loops will keep executing as long as the condition remains true.

count = 5
while count > 0:
  print(count)
  count = count - 1

Section 4 – Object oriented programming

OOP makes code modular with the help of classes and objects. A class is like a blueprint while objects are instances of the class created from it.

Here is an example of a Python class and object:

class Vehicle:
   def __init__(self, make, color):
      self.make = make 
      self.color = color

   def printDetails(self):
      print(self.make, self.color)

car = Vehicle("Toyota", "Grey")  
car.printDetails()

The self parameter refers to current instance while _init_ is the constructor.

Section 5 – Python File Handling

Python provides inbuilt functions to read and write files.

Opening file in read mode

file = open("data.txt", "r") 

This opens data.txt file in read mode.

Writing to a file

file = open("data.txt", "w")
file.write("This text will override existing data") 
file.close()

This will overwrite any existing content.

You can also append to a file without overriding using "a" mode.

There are many more functions available for handling files like move, delete, folder creation etc. They allow you to have input/output operations for files and directories.

Summary

This tutorial PDF provided an introduction to Python programming covering everything from basic syntax, conditional statements, loops, functions to file handling and OOPs with coding examples and explanations. Mastering these core concepts will give you a strong foundation to build advanced projects in Python. The concise examples illustrate each element for faster learning. Keep practicing the programs to become proficient.

Read More Topics