PYTHON REVISION 5) IntegraƟon CapabiliƟes:
Easily integrates with other
TOUR FOR CLASS 12 languages.
GETTING STARTED WITH 6) Rapid Development : Quick
PYTHON prototyping and
development.
7) Cross-Plaƞorm:CompaƟble
with major operaƟng systems.
IntroducƟon to Python: 8) Scalability: Can be used in
Python is aninterpreted, large-scale applicaƟons.
high-level, and general-
purpose programming Disadvantages of Python:
language.
It emphasizes code readability 1) Speed:Interpreted nature
with its notable use of may be slower than compiled
significant indentaƟon. languages.
2) Global InterpreterLock(GIL):
Features of Python: Limits thread execuƟon on
Easytolearn and use. mulƟ-core systems.
Open-source and free. : Not the primary choice for
3) MobileDevelopment
Extensive library support. mobile app development.
Plaƞorm-independent.
4) Design RestricƟons: Some
Advantages of Python: developers may find design
philosophies limiƟng.
1) Readability: Clear and easy- 5) MemoryConsumpƟon:May
to-understand syntax. use more memory, especially
2) LargeStandardLibrary: in resource-constrained
Extensive pre-built modules environments.
for various tasks. 6) Threading LimitaƟons :
3) VersaƟlity:Suitable for web Challenges in leveraging
development, data science, mulƟ-core processors.
and more. 7) Packaging Issues: Managing
4) Community Support : AcƟve dependencies can be
community providing challenging.
resources and libraries. 8) LessSuitableforResource-
Intensive Tasks:Performance
may not match languages 3) Literals:
designed for resource- Fixedvalues in a program.
intensive tasks Typesof literals:
Numeric Literals:
TOKENS Integers, floats,
complex numbers.
IntroducƟon to Tokens: x = 10
Tokens are the smallest units y = 3.14
of a Python program. They z = 2 + 3j
are the building blocks of a i. String Literals:
Python script. s = "Hello"
ii. Boolean Literals:
Types of Tokens: True and False.
1) Keywords: status = True
Reserved words in Python 4) Operators:
that have predefined Operators are special symbols or
meanings. keywords in Python that perform
Examples: if, else, while, operations on operands. Operands
for, import, True, False. are the values or variables on which
if True: the operation is performed. Python
print("This is akeyword example") supports a wide range of operators
classified into several categories.
2) IdenƟfiers:
Namesused to idenƟfy Types of Operators:
variables, funcƟons, a) ArithmeticOperators :
classes,etc. Must begin These operators are
with a leƩer (A-Z ora-z) or used to perform
mathematical
an underscore (_),
operations.
followed by leƩers, digits
Operato
(0-9), or underscores. r
Description Example
Examples: my_var, _temp, →8
+ Addition 5+3
Counter. -* Subtraction 10 - 4 → 6
/ Multiplication 6 * 3 → 18
my_var = 10 // Division 15 / 2 → 7.5
print(my_var) % Floor Division 15 // 2 → 7
Modulus
(Remainder) 15 % 4 → 3
Operato used to assign values to
Description Example
r variables.
2 **
3
Exponentiatio Operator Description Example
** →
n
8 = Assign x = 10
+= Add and assign x += 5 →
b) Relational 15
Subtract and assign x -= 3 →
(Comparison) -=
12
Operators:These Multiply and assign x *= 2 →
*=
operators compare two 24
values and return a /= Divide and assign x /= 4 → 6
Boolean (True or False). Floor divide and
//= x //= 2 → 3
Operator Description Example assign
== 5 == 5 → Exponent and
Equal to True **= x **= 2 → 9
assign
5 != 3 → Modulus and
!= Not equal to True %= x %= 2 → 1
assign
7 > 5→
> Greater than True
3<5 e) Bitwise Operators:
→
< Less than True These operators
Greater than or
5 >= 5
→
perform operations at
>= True the bit level.
equal to
4 <= 5
Less than or equal True → Operator Description Example
<=
to & AND 5&3→1
` ` OR
c) Logical Operators: ^ XOR 5^3→6
These operators are ~ NOT ~5 → -6
used to combine << Left Shift 5 << 1 → 10
conditional statements. 5 >> 1 →
>> Right Shift
Operator Description Example 2
and Returns True if (5 > 3) and (6
both are true > 4) → True f) Membership
Returns True if (5 > 3) or (3 > OperatorsThese
or
one is true 7) → True
operators test
Reverses the not(5 > 3) →
not membership in a
Boolean result False sequence (e.g., list,
string).
d) Assignment Operators:
Theseoperators are
Operator Description Example
in True if present 'a' in 'apple' → Strings
True
IntroducƟon:
True if not 'x' not in
not in Strings in Python are
present 'apple' → True
sequences of characters
g) Identity Operators enclosed in quotes.
Theseoperators s ="Hello"
compare memory
locations of two String OperaƟons:
objects.
Operator Description Example ConcatenaƟon:
is True if same object x is y "Hello"+ " World"
True if not the same x is not #Output: "Hello World"
is not object y
RepeƟƟon:
Python ExecuƟon Modes: "Hi"*3
InteracƟve Mode: Commands #Output:"Hi Hi Hi "
are executed one at a Ɵme in
a Python shell. Membership:
Script Mode: A program is 'a'in'apple'
wriƩen in a file with .py #Output: True
extension and executed.
String Slicing:
Basic Syntax: Slicing allows you to extract
porƟons of a string using
Comments: indices.
#Thisisacomment Syntax:
string[start:stop:step]
Variables:
x = 10 Examples:
y = "Hello"
s="Hello,World!"
Example Program:
print("Hello, Python!") print(s[1:5])
x=5 #Output:"ello"
print(x * 2)
# Output:10 print(s[:5])
#Output:"Hello" (start is omiƩed, upper(): Converts the string
default is0) to uppercase.
print("hello".upper())
print(s[7:]) #Output: "HELLO"
#Output:"World!" (stop is omiƩed,
goes Ɵll theend) strip(): Removes leading and
trailing spaces.
print(s[::2]) print(" hello ".strip())
#Output:"Hlool!" (every second # Output: "hello"
character)
replace(): Replaces a
print(s[::-1]) substring with another
#Output: "!dlroW ,olleH" (reverses substring.
the string) print("apple".replace("a", "A"))
# Output: "Apple"
NegaƟve Indexing:
NegaƟve indices count from the find(): Returns the index of
end of the string. the first occurrence of a
print(s[-1]) substring.
# Output: "!" (last character) print("banana".find("na"))
#Output: 2
print(s[-6:-1])
# Output: "World" (slicing with Example Program:
negaƟve indices) s = "Hello, World!"
print(s.lower())
Built-in String Methods:
len():Returns the length of Lists
the string. Int roducƟon:
print(len("Hello")) A list is a collecƟon of items,
# Output: 5 which can be of different data
types.
lower(): Converts the string to
lowercase. lst = [1, 2, "Apple", 3.5]
print("HELLO".lower())
#Output: "hello" List OperaƟons:
ConcatenaƟon:
[1, 2] + [3, 4] insert(): Inserts an element at
#Output:[1,2, 3, 4] aspecifiedposiƟon.
lst.insert(2, "New")
RepeƟƟon:
["Hi"] * 3 remove(): Removes the first
#Output:["Hi", "Hi", "Hi"] occurrence of a value.
lst.remove(3)
Membership:
3 in [1, 2, 3] pop(): Removes and returns
#Output:True the element at the specified
posiƟon.
List Slicing: lst.pop(1)
Similartostringslicing.
lst = [1, 2, 3, 4, 5] sort(): Sorts the list in
ascending order.
print(lst[1:4]) lst.sort()
# Output: [2, 3, 4]
reverse(): Reverses the order
print(lst[:3]) ofelements in the list.
#Output:[1,2, 3] lst.reverse()
print(lst[3:]) ExampleProgram:
#Output:[4,5] lst = [1, 3, 2]
lst.sort()
print(lst[::-1]) print(lst)
#Output:[5,4,3, 2, 1] (reverses #Output:[1,2, 3]
thelist)
Built-in List Methods:
append(): Adds an element to
theend of the list.
lst.append(6)
extend(): Adds elements of Tuples
another list.
lst.extend([7, 8]) IntroducƟon:
A tuple is a collecƟon of print((1, 2, 1).count(1))
immutable items. #Output: 2
tpl =(1,2,"Apple")
index(): Returns the index of
Tuple OperaƟons: the first occurrence of a
ConcatenaƟon: value.
(1, 2) + (3, 4) print((1, 2, 3).index(2))
# Output: (1, 2, 3, 4) # Output: 1
RepeƟƟon: ExampleProgram:
("Hi",)*3 tpl = (1, 2, 3)
# Output: ("Hi", "Hi", "Hi") print(tpl.index(2))
#Output: 1
Membership:
3 in (1, 2, 3)
# Output: True
DicƟonary
Tuple Slicing: Int roducƟon:
Similar to lists. A dicƟonary is a collecƟon of
tpl=(1,2,3, 4, 5) key-value pairs.
print(tpl[1:4]) dct ={"name": "Alice", "age": 25}
#Output: (2, 3, 4)
DicƟonary OperaƟons:
print(tpl[:3]) AccessingValues:
#Output: (1, 2, 3) print(dct["name"])
# Output: "Alice"
print(tpl[3:])
#Output: (4, 5) Adding Items:
dct["city"] = "New York"
print(tpl[::-1])
#Output: (5, 4, 3, 2, 1) (reverses Removing Items:
thetuple) dct.pop("age")
Built-in Tuple Methods: Built-in DicƟonary Methods:
count(): Counts the
occurrences of a value.
keys(): Returns all keys in the
dicƟonary.
print(dct.keys())
values(): Returns all values in
the dicƟonary.
print(dct.values())
items(): Returns all key-value
pairs as tuples.
print(dct.items())
get(): Returns the value for a
specified key.
print(dct.get("name"))
# Output: "Alice"
update(): Updates the
dicƟonary with key-value
pairs from another dicƟonary.
dct.update({"age": 26})
clear(): Removes all items
fromthe dicƟonary.
dct.clear()
Downloaded by Chikuu (chikuu0012@gmail.com)