UNIT 2 Python
UNIT 2 Python
Execu on Modes:
o Interac ve Mode:
The user writes and executes Python code line by line in an interpreter
(like Python Shell or an IDE).
o Script Mode:
Structure of a Program:
o Common structure: Impor ng modules, func on defini ons, and the main
code logic.
Indenta on:
o Iden fiers: Names used for variables, func ons, classes, etc. Examples: x,
total_price, my_func on.
o Constants: Values that do not change during the program execu on (e.g., PI =
3.14). Usually wri en in uppercase le ers.
o Variables: Names used to store data values which can change during the
program execu on.
Types of Operators:
o Rela onal Operators: ==, !=, >, <, >=, <= (used to compare values).
o Logical Operators: and, or, not (used for logical condi ons).
o Assignment Operators: =, +=, -=, *=, /=, etc. (used to assign values).
o Bitwise Operators: &, |, ^, ~, <<, >> (used to perform bitwise opera ons).
Precedence of Operators:
2. Data Types
Statements: Instruc ons that the Python interpreter executes (e.g., variable
assignments, func on calls).
Single-line Comment:
python
# This is a comment
python
python
o The input() func on always returns a string, which can be converted to other
data types.
python
print("Hello", name)
python
python
7. Debugging
Debugging: The process of iden fying and fixing errors in the program.
o Print Statements: Used to check the values of variables and follow the
program's flow.
o Use of IDEs: Many IDEs (like PyCharm) provide built-in debuggers to step
through code and inspect variables.
Control Statements
python
if condi on:
# Code block
Example:-
age = 18
python
else:
# Code block
Example:-
age = 16
else:
elif Statement: Checks addi onal condi ons if the previous if condi on was false.
python
if condi on1:
# Code block
# Code block
else:
# Code block
Example:-
age = 25
else:
python
# Code block
Example:-
count = 0
print(count)
python
for i in range(5):
# Code block
Example:-
for i in range(5):
print(i)
# Output: 0 1 2 3 4
Lists
python
list1 = [1, 2, 3]
Example:-
len(list):
python
my_list = [1, 2, 3, 4]
print(len(my_list)) # Output: 4
list.append(item):
python
my_list = [1, 2, 3]
my_list.append(4)
list.insert(index, item):
python
my_list = [1, 2, 3]
my_list.insert(1, 6)
python
my_list = [1, 2, 3, 4]
my_list.remove(3)
list.pop(index):
python
my_list = [1, 2, 3, 4]
popped_item = my_list.pop(2)
print(popped_item) # Output: 3
list.reverse():
python
my_list = [1, 2, 3, 4]
my_list.reverse()
list.sort():
python
my_list = [4, 2, 3, 1]
my_list.sort()
list.count(item):
python
my_list = [1, 2, 2, 3, 2]
print(my_list.count(2)) # Output: 3
list.index(item):
python
my_list = [1, 2, 3, 4]
print(my_list.index(3)) # Output: 2
python
my_list = [1, 2, 3, 4]
print(min(my_list)) # Output: 1
print(max(my_list)) # Output: 4
print(sum(my_list)) # Output: 10
Dic onary
Key-Value Pair: A dic onary stores data as pairs of unique keys and values.
python
Example:-
dict.keys():
python
python
dict.items():
python
dict.update():
python
del dict[key]:
python
del my_dict["age"]
dict.clear():
python
my_dict.clear()
print(my_dict) # Output: {}