Python Swapnil s1
Python Swapnil s1
2. Main Content
2.1.1 Simplicity
Python is designed to be easy to read and write, resembling the English language.
It reduces the learning curve for beginners and speeds up development.
2.1.5 Portability
2.3.1 Variables
x = 5 # Integer
y = "John" # String
z = 3.0 # Float
s = "apple"
print(s[2:4]) # Output: "pl"
print(s[:3]) # Output: "app"
2.3.5 Tuple
2.3.6 Dictionary
2.3.7 Set
Sets store unique elements only.
unique_numbers = {1, 2, 2, 3}
print(unique_numbers) # Output: {1, 2, 3}
2.4 Operators
Operators in Python include arithmetic (+, -, *, /), assignment (=, +=, -=), and
comparison (==, !=, <, >) operators.
2.5 Control Flow
Python supports structured decision-making and repetition.
x = 10
if x > 5:
print("Greater than 5")
else:
print("Less than or equal to 5")
Elif Example:
if x > 10:
print("Greater than 10")
elif x == 10:
print("Equal to 10")
else:
print("Less than 10")
2.5.2 Looping
For Loop:
for i in range(5):
print(i)
While Loop:
i = 0
while i < 5:
print(i)
i += 1
2.6 Functions
Definition: Functions allow you to group code into reusable blocks.
Usage:
try:
x = int("abc")
except ValueError:
print("Invalid value")
import csv
with open('data.csv', mode='r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
5. Additional Resources
PyPI: PyPI