Python Concepts Notes (Simple Format)
Used to store data.
No need to declare type.
Syntax:
int → whole numbers
float → decimals
str → text
bool → True/False
list → ordered collection
tuple → fixed collection
dict → key-value pairs
set → unique items
Convert one type to another.
int ("5" )
float (10 )
str (20 )
Input always comes as string.
name = input ("Enter name: " )
print (name )
Arithmetic: +, -, *, /, %, //, **
Comparison: ==, !=, >, <, >=, <=
Logical: and, or, not
Assignment: =, +=, -=, *=, /=
Used for decision-making.
if condition :
# code
elif condition :
# code
else :
# code
Used when you know how many times to repeat.
for i in range (5 ):
print (i )
Runs until condition becomes false.
break → stop loop
continue → skip iteration
arr = [1 , 2 , 3 ]
arr .append (4 )
arr .remove (2 )
student = {"name" : "Shreyas" , "age" : 21 }
print (student ["name" ])
def add (a , b ):
return a + b
Small anonymous function.
Reuse code from other files.
f = open ("data.txt" , "r" )
data = f .read ()
f .close ()
try :
x = 10 / 0
except ZeroDivisionError :
print ("Cannot divide" )
class Car :
def __init__ (self , brand ):
self .brand = brand
c = Car ("BMW" )
class A :
pass
class B (A ):
pass
Class
Object
Encapsulation
Abstraction
Inheritance
Polymorphism
nums = [x for x in range (5 )]
def gen ():
yield 1
yield 2