Python Tutorials
Introduction to Python
Python is a high-level, interpreted programming language known for its simplicity and readability. It
was created by Guido van Rossum and first released in 1991. Python emphasizes code readability
with its notable use of significant whitespace.
Page 1
Python Tutorials
Installing Python
To install Python, visit the official website [Link] and download the latest version for your
operating system. Follow the installation instructions. You can verify the installation by running
python --version in your terminal.
Page 2
Python Tutorials
Variables and Data Types
Variables are used to store data. In Python, you don't need to declare the type. Python has several
built-in data types: int, float, str, bool, list, tuple, dict.
Example: x = 5 # int
y = 3.14 # float
name = "Python" # str
Page 3
Python Tutorials
Operators
Python supports various operators: arithmetic (+, -, *, /, %), comparison (==, !=, <, >), logical (and,
or, not), assignment (=, +=, -=).
Example: a = 10 + 5 # 15
b = a > 10 # True
Page 4
Python Tutorials
Control Structures: If Statements
If statements allow conditional execution. Syntax: if condition: statements elif condition: statements
else: statements
Example:
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")
Page 5
Python Tutorials
Loops: For Loop
For loops iterate over sequences. Syntax: for item in sequence: statements
Example:
for i in range(5):
print(i) # Prints 0 to 4
Page 6
Python Tutorials
Loops: While Loop
While loops execute as long as condition is true. Syntax: while condition: statements
Example:
i=0
while i < 5:
print(i)
i += 1
Page 7
Python Tutorials
Functions
Functions are reusable blocks of code. Syntax: def function_name(parameters): statements return
value
Example:
def greet(name):
return f"Hello, {name}!"
print(greet("World"))
Page 8
Python Tutorials
Lists
Lists are mutable sequences. Syntax: my_list = [item1, item2, ...]
Operations: append(), remove(), sort(), len()
Example:
fruits = ["apple", "banana", "cherry"]
[Link]("orange")
print(fruits)
Page 9
Python Tutorials
Dictionaries
Dictionaries store key-value pairs. Syntax: my_dict = {"key": "value"}
Operations: keys(), values(), items(), get()
Example:
person = {"name": "John", "age": 30}
print(person["name"])
Page 10
Python Tutorials
File Handling
To work with files: open() function. Modes: r (read), w (write), a (append)
Example:
with open("[Link]", "w") as f:
[Link]("Hello, World!")
Page 11
Python Tutorials
Exception Handling
Handle errors with try-except blocks.
Example:
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero")
Page 12