Class 12 CS Class 1PRT
Class 12 CS Class 1PRT
Arora
Lovejeet Python
Identifiers Rule
Tokens
Revision
Error Finding Question
Tour
Arora
Expression Type
Slicing
Revision
Return type of inbuilt function
Tour
Arora
Concatenation/Replication
List Programming
Arora
Lovejeet
Let’s Begin
Python Revision Tour
Arora
Lovejeet
A Dutch programmer named Guido van Rossum
made Python in 1991. He named it after the television show
Monty Python's Flying Circus.
Arora
A. Extensive support libraries(NumPy for numerical calculations,
Lovejeet
Pandas for data analytics etc)
B. Open source
Advantages
C. Easy to learn
D. User-friendly data structures
E. High-level language
F. Dynamically typed language(No need to mention data type based
Arora
on value assigned, it takes data type)
G. Object-oriented language
H. Portable across Operating systems
Lovejeet
Arora
Lovejeet
Interactive Mode
Arora
Lovejeet
Interactive Mode
Arora
Lovejeet
Script Mode
Arora
Lovejeet
Execution Modes There are two ways to run a
program using the Python interpreter:
a) Interactive mode
Arora
b) Script mode
Lovejeet
a) Interactive Mode: In this mode, you can directly interact with the Python interpreter prompt (usually denoted
by >>>). You type commands and get immediate results. It's useful for testing small pieces of code, experimenting,
or learning Python interactively.
…………………………………………………………………………………………………………………………………….
b) Script Mode: In this mode, you write your Python code in a script file (usually with a .py extension) using a text
editor or an Integrated Development Environment (IDE). Then, you run the script using the Python interpreter either
from the command line or from within an IDE. The interpreter reads the code from the file and executes it sequentially.
Arora
Lovejeet
“Operators
Arora 14
Lovejeet
Operators:
used to perform
specific
Arora
operation.
……………………………
Lovejeet
Binary
Unary
Types
Of
Operator
Arora M A R K E T S T A T U S A N A L Y S I S
Arithmetic Operator
Lovejeet
Binary
Assignment Operator
Comparison Operator
Logical Operator
Bitwise Operator
Lovejeet
+ Addition Adds values on either side of the operator. a + b = 30
Arora
// Floor Division - The division of operands where 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -
the result is the quotient in which the digits after 4, -11.0//3 = -4.0
the decimal point are removed. But if one of the
operands is negative, the result is floored, i.e.,
rounded away from zero (towards negative
infinity) −
Arithmetic Operator
Source →https://www.w3schools.com/
Lovejeet
D/F B/W
* and **
Arora
Lovejeet
D/F ☺ B/W
/ and //
Arora
Lovejeet
D/F B/W
= and ==
Arora
Operator Description Example
Lovejeet
= Assigns values from right side operands to left side
c = a + b assigns value of a + b into c
operand
+= Add AND It adds right operand to the left operand and assign the
c += a is equivalent to c = c + a
result to left operand
-= Subtract AND It subtracts right operand from the left operand and
assign the result to left operand c -= a is equivalent to c = c - a
*= Multiply AND It multiplies right operand with the left operand and
assign the result to left operand c *= a is equivalent to c = c * a
/= Divide AND It divides left operand with the right operand and assign
the result to left operand c /= a is equivalent to c = c / a
Arora
%= Modulus AND It takes modulus using two operands and assign the result
to left operand c %= a is equivalent to c = c % a
//= Floor Division It performs floor division on operators and assign value
to the left operand c //= a is equivalent to c = c // a
Assignment Operator
Source →https://www.w3schools.com/
Operator Name Example
Lovejeet
==
Equal x == y
!=
Not equal x != y
>
Greater than x>y
<
Less than x<y
Arora
>=
Greater than or equal to x >= y
<=
Less than or equal to x <= y
Comparison Operator
Source →https://www.w3schools.com/
Operator Description Example
Lovejeet
Returns True if both statements are true x < 5 and x < 10
and
Returns True if one of the statements is true x < 5 or x < 4
or
Reverse the result, returns False if the result is not(x < 5 and x < 10)
not true
Logical Operator
Arora
Source →https://www.w3schools.com/
Operator Description Example
Lovejeet
Returns True if both variables are the same x is y
is object
Returns True if both variables are not the x is not y
is not same object
Identity Operator
Arora
Source →https://www.w3schools.com/
Operator Description Example
Lovejeet
Returns True if a sequence with the specified x in y
in value is present in the object
Returns True if a sequence with the specified x not in y
not in value is not present in the object
Membership Operator
Arora
Source →https://www.w3schools.com/
Lovejeet
❑ () Para thesis
❑ ** Exponents
Priority(High to Low)
❑ ~ Bitwise Compliment
❑ +- (Unary +_ )
❑ *, /, //, %(Multiply, Division, Floor Division, Reminder)
❑ +, - (Binary Addition, Binary Subtraction)
❑ & (Bitwise and)
Exponent
Arora
❑ ^ Bitwise XOR
❑ !(Bitwise OR
❑ <,<=,>,>=,!=,==(Relational Operator) Exponent **(Right to Left)
❑ Is, is not (Identity Operator)
❑ Not Logical Not
❑ And Logical and
❑ Or Logical or
❑ =, +=, -=, /=,%=,**=(Assignment Operator)
Python Operators
Lovejeet
Precedence
Arora
print(11 * 3 % 5 )
Lovejeet
1.1
2.2
3.3
Arora
4.4
5.2.0
print(11 * 3 % 5 )
Lovejeet
1.1
2.2
3.3
Arora
4.4
5.2.0
print((2 * 6) / 4-1)
Lovejeet
1.2
2.2.0
Arora
3.4
4.4.0
5.12
print((2 * 6) / 4-1)
Lovejeet
1.2
2.2.0
Arora
3.4
4.4.0
5.12
print(12 % 5 * 3 )
Lovejeet
1.2
2.2.0
Arora
3.6
4.6.0
5.12
print(12 % 5 * 3 )
Lovejeet
1.2
2.2.0
Arora
3.6
4.6.0
5.12
Lovejeet
Python Tokens
Arora
Smallest unit of program
Lovejeet Keyword
1. Python keywords are special reserved words.
2. They convey unique meanings to the compiler/interpreter.
Arora
3. Each keyword has a specific task.
4. Keywords should not be used as variable names.
5. Keywords are case-sensitive.
36
Lovejeet
Keywords in Python
“
False class finally is return
Arora
and del global not with
as elif if or yield
Arora
Braces: ( ), { }, [ ] and
Comma ‘ , ‘
Lovejeet
“
\
Identifiers
”
are the name used to give an identity a variable, function, class,
object, arrays etc.
1. Identifiers can consist of lowercase letters (a to z), uppercase letters (A to Z), digits
(0 to 9), or underscores (_). Examples include `myClass`, `var_1`, and
Arora
`print_this_to_screen`.
2. An identifier cannot start with a digit. For instance, `1variable` is invalid, but
`variable1` is a valid name.
3. Keywords cannot be used as identifiers.
4. Special symbols like `!`, `@`, `#`, `$`, `%`, etc., cannot be used in identifiers.
5. Spaces are not allowed in identifiers. 39
Arora
variable?
Lovejeet
Variable data=50;//Here data is variable
Variable is name of reserved area allocated in memory. In other words, it is a name of
memory location. It is a combination of "vary + able" that means its value can be changed.
Arora
Lovejeet
Variable is a reserved memory
location to store values. In other
words, a variable in a python
Arora
program gives data to the
computer for processing.
Lovejeet
It is a container where we can put our value.
X=10
Arora 43
Lovejeet
“
Variables are
case sensitive
in python.
Arora
Lovejeet
“ Multiple
Arora
Assignment
Lovejeet
“ Multiple
Arora
Assignment
a, b, c = 10, 20, 30
Lovejeet
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c, end = '')
print ('p, q, r :', p, q, r)
Arora
a, b, c = 10, 20, 30
Lovejeet
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c, end = '')
print ('p, q, r :', p, q, r)
(i) a, b, c : 10 20 30p, q, r : 25 13 16
Arora
(ii) a, b, c : 25 13 16, q, r : 25 13 16
(iii) a, b, c : 25 13 16 p, q, r : 10 20 30
(iv) a, b, c : 10 20 30p, q, r : 10 20 30
a, b, c = 10, 20, 30
Lovejeet
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c, end = '')
print ('p, q, r :', p, q, r)
(i) a, b, c : 10 20 30p, q, r : 25 13 16
Arora
(ii) a, b, c : 25 13 16, q, r : 25 13 16
(iii) a, b, c : 25 13 16 p, q, r : 10 20 30
(iv) a, b, c : 10 20 30p, q, r : 10 20 30
A. DATE_9_7_77=10
Lovejeet
B. DRY_100=100
C. Police-100=100
D. NUmber_n=90
(i) All are Correct
Arora
(ii) All are Incorrect
(iii) Only D Is Incorrect
(iv) Only C Is Incorrect
(v) D and C Both Incorrect
A. DATE_9_7_77=10
Lovejeet
B. DRY_100=100
C. Police-100=100
D. NUmber_n=90
(i) All are Correct
Arora
(ii) All are Incorrect
(iii) Only D Is Incorrect
(iv) Only C Is Incorrect
(v) D and C Both Incorrect
Lovejeet
identify the Comparison Operator
A. !=
B. =
C. in
D. is
Arora
E. +=
Lovejeet
identify the Comparison Operator
A. !=
B. =
C. in
D. is
Arora
E. +=
Lovejeet
Data
Arora
Types
Lovejeet
“
Variables can hold values of different data types. Python is a dynamically
typed language hence we need not define the type of the variable
Arora
while declaring it. The interpreter implicitly binds the value with its type.
55
Lovejeet
“
Arora
Lovejeet
“
Arora
Lovejeet
a=10
b=45.90
c=True
d="Love"
e='C'
Arora
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
[] a=[1,2,3,"k"]
Lovejeet
List
b={"A":1,"B":5}
c=(1,2,3,"4")
Tuple ()
d="Rabbit"
print(type(a))
String “”
Arora
print(type(b))
print(type(c))
Dict {} print(type(d))
Lovejeet
Mutable List and
(Change) Dictionary
Integer, Float,
Immutable
String. Complex,
(unchanged)
Arora
Bool, Tuple
Lovejeet
Type Casting
Implicit
Explicit
Arora
▪ By Default
▪ By Interpreter
▪ By compiler
▪ By Programmer
or User
Lovejeet
Implicit
Arora
▪ By Default
▪ By Interpreter
▪ By compiler
Lovejeet
Explicit
Arora
▪ By Programmer
or User
Lovejeet
Find the valid identifiers from the following:
(a) Date_21
(b) 21Net
(c) continue a)A ,c and e
(d)_final
(e) For b)B and E
Arora
c)B and C
d)A, D and E
Lovejeet
Find the valid identifiers from the following:
(a) Date_21
(b) 21Net
(c) continue a)A ,c and e
(d)_final
(e) For b)B and E
Arora
c)B and C
d)A, D and E
Lovejeet
Python Statements
Arora
………………………………………
Lovejeet
Flow of Control
Sequence
Selection
Arora Iteration
Jumping
Lovejeet
Arora
Sequence
Lovejeet
Arora
Selection
Lovejeet
Arora
Iteration
Lovejeet
Arora
Lovejeet
Jumping
Break
Arora Continue
Lovejeet
Python Decision Making
Arora
if
Lovejeet
True
Test
Condition
AroraBody of if
Lovejeet
Arora
if
Lovejeet
False
Test
Condition
Arora Body of if
Lovejeet
Arora
Else
Lovejeet
True
Test
Condition
AroraBody of if
Lovejeet
Arora
Else
Lovejeet
False
Test
Condition
Arora Body of if
Body of else
Number=int(input("Enter a Numeber to Check even ODD"))
Lovejeet
if (Number%2==0):
print("Even")
else:
print("Odd")
Basic code
Arora
for if else
elif
Lovejeet
Test Condition1
Test Condition2
True
True
Stm1
Stm2
Arora
Test Condition…n
False
True
Else
Stm3
Lovejeet
Arora
elif
Lovejeet
marks=int(input("Enter the marks:"))
if marks>=90:
print("A")
elif(marks>=80):
C:\Users\LovejeetHP\venv\Scripts\python.exe
print("B") C:/Users/LovejeetHP/PycharmProjects/test/Love.py
Enter the marks:70
elif(marks>=70):C
Arora
print("C") Process finished with exit code 0
elif(marks>=60):
print("D")
else:
print("E")
if (3>9): if (3>9):
Lovejeet
if (3>9):
print("Hello") print("Hello") print("Hello")
else: elif(9>8):
print("Bye") print("Bye")
Arora 85
Lovejeet
if (3>9): if (3>9):
print("Hello") print("Hello")
elif(9>8): elif(9>8):
print("Bye") print("Bye")
elif(6>9): elif(6>9):
Arora
print("Ok") print("Ok")
else:
print("I am Else")
if(True):
Lovejeet
pint("i am If")
if(1):
pint("i am If")
if(-9):
pint("i am If")
Output
i am If
Arora
if("Sonal"):
print("i am If")
if(9.99):
print("i am If")
Lovejeet
if(False):
print("i am If")
if(0):
print("i am If") Output
No output
Arora
if():
print("i am If")
if(""):
print("i am If")
Lovejeet
num = float(input("Enter a number: ")) N
if num >= 0: e
if num == 0:
print("Zero") s
else: t
Arora
print("Positive number")
else: e
print("Negative number") d
if
Lovejeet
a="23"
c=a*2.3
d=a*2
1.
2.
3.
23.23.23
Error
23.2.3
Arora
print(c) 4. 23,2323
5. 23
print(d)
Lovejeet
a="23"
c=a*2.3
d=a*2
1.
2.
3.
23.23.23
Error
23.2.3
Arora
print(c) 4. 23,2323
5. 23
print(d)
x
S="String"
Lovejeet
s1=list(S)
print(s1)
1. ['S', 't', 'r', 'i', 'n', 'g’]
2. 'S', 't', 'r', 'i', 'n', 'g’
3. [‘String’]
Arora
4. Error
x
S="String"
Lovejeet
s1=list(S)
print(s1)
1. ['S', 't', 'r', 'i', 'n', 'g’]
2. 'S', 't', 'r', 'i', 'n', 'g’
3. [‘String’]
Arora
4. Error
print(True+False+True+2)
1.
2.
3.
Lovejeet
1
2
4
Arora
4. True
5. False
print(True+False+True+2)
1.
2.
3.
Lovejeet
1
2
4
Arora
4. True
5. False