■ Python Full Syntax Notes (Beginner to Master)
■ Python Keywords
False, True, None, and, or, not, if, elif, else,
for, while, break, continue, pass,
def, return, class, import, from, as,
try, except, finally, raise, assert,
with, lambda, yield, global, nonlocal, in, is
■ Variables & Data Types
x = 10 # int
y = 3.14 # float
name = "Ali" # str
flag = True # bool
nums = [1,2,3] # list
tup = (1,2,3) # tuple
s = {1,2,3} # set
d = {"a":1} # dict
■ Operators
+, -, *, /, //, %, **
==, !=, >, <, >=, <=
and, or, not
in, is
■ Input & Output
name = input("Enter name: ")
print("Hello", name)
■ Conditional Statements
if x > 10:
print("Big")
elif x == 10:
print("Equal")
else:
print("Small")
■ Loops
for i in range(5):
print(i)
while x > 0:
x -= 1
■ Functions
def greet(name):
return "Hello " + name
def func(a, b=0, *args, **kwargs):
return a + b
■ Classes & Objects
class Person:
def __init__(self, name):
self.name = name
p = Person("Ali")
print(p.name)
■ Exception Handling
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Done")
■ File Handling
with open("file.txt", "w") as f:
f.write("Hello")
with open("file.txt", "r") as f:
data = f.read()
■ Useful Built-ins
len(), range(), type(), str(), int(), float(),
list(), dict(), set(), sum(), min(), max(), sorted()
■ Important Modules
import math
math.sqrt(16), math.pi
import random
random.randint(1,10)
import datetime
datetime.datetime.now()
import os
os.getcwd(), os.listdir()
■ Advanced
# Lambda
square = lambda x: x*x
# List Comprehension
squares = [x*x for x in range(5)]
# Decorator
def deco(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper