Python Notes
Python Notes
For example :- a = 10
Where a is a variable and 10 is a value which store named a variable.
Note: In python jab bhi hum koi value assign krte hai kisi bhi variable ko
to jo isme memory allocation hota hai vo value ka hota hai na ki variable.
Aur other language like java, c++ etc isme variable ka memory allocation
hota h.
Eg: a=10
B=10
Agar hum yaha dekhenge to dono ka memory address same hoga kyunki
yaha value ka memory allocation hua aur vo a aur b dono ko point kr rha
h.
Logical Operator: - If we have more than one condition then we have to use
logical operator. Below are the logical operator:
Operator Description Example
and Returns true if both X<5 and x<10
statements are true
or Returns true if one of X<5 or x<4
the statements are true
not Reverse the result, not(x<5 and x<10)
returns False if the
result is true
Membership Operator: - it is used in data type we will look into data type
in more details.
Operator Description Example
in Returns True if a X in Y
sequence with the
specified value is
present in the object
not in Returns True if a X not in Y
sequence with the
specified value is not
present in the object.
Identity Operator: - There are basically two operator is and is not it is like
== and !=
Operator Description Example
is Returns True if both X is Y
variables are the same
object
is not Returns True if both X is not Y
variables are not the
same object
Bitwise Operator: - Bitwise operator work bit, in bit there are two value 0
and 1.
Operator Name Description
& AND X&Y
| OR X|Y
^ XOR X^Y
Truth table
1=True 0= False
A B A&B A|B A^B
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
String:
A String is a collection of one or more characters put in a single quote, double-
quote or triple quote. Multi-line strings can be denoted using triple quotes, ‘’’or’’’
Eg. S=”Hello@123”
S =’’’Hello abhishek, what are
you doing’’’
List:
List is an ordered sequence of items. It is one of the most used datatypes in
python and is very flexible. It is denoted under the [ ] bracket.
Eg. A=[1,2.2,’ws’]
Tuple:
Tuple is an ordered sequence of items same as a list. It is defined within
parentheses () where items are separated by commas.
“The deference between list and tuple is that tuple is immutable and faster list where list
mutable and slower than tuple.”
Eg. T=(5,’progam’,1+3j)
Note: If we put only one value inside parentheses then it is not considered under the
tuple so for tuple it is important more than one value under the parentheses. Means
comma separated value must be inside the ().
Dictionary:
Dictionary is an unordered collection of key-value pairs. In python,
dictionaries are defined within braces {} with each item being a pair in the form
key:value.
Eg. D={1:’value’,’key’:2}
Print(type(d))
<class ‘dict’>
Set:
A set is an unordered collection of items. Every set element is unique (no
dublicates) and must be immutable (cannot be change). It also denoted under {}.
Eg. - My_set={1,2,3}
Print(My_set)
Getting User Input & Type casting:
Syntax:
A = input(“Enter the value:-”)
print(a)
it can take the input at the run time from the user. By default the values are in string. If
we want to take values another datatype then need to do typecasting.
If we don’t know that what we want integer or float type value then we use eval, It can
handle both integer and float type value. And also handle binary data type.
A= eval(input(“Enter the value”))
Conditional Statements:
A Conditional statement as the name suggests itself, is use to handle conditions in
our program. There are three types of conditional statement:
1. If Statement
2. If else statement
3. If elif else statement
Eg. A=10
If a%2==0:
print(“Even Number”);
else:
print(“Odd Number”);
is elif else Statement: Is we have more than one condition then we use this condition.
Syntax: if [conditional expression]:
[statement(s) to execute]
elif[conditional expression]:
[statement(s) to execute]
else:
[statement(s) to execute]
E.g per = 55
If per >=60:
print(“first Division”)
elif per >=48:
print(“second Division”)
elif per >=35:
print(“Third Division”)
else:
print(“fail”)
#range(5)
#start=0 by default
#condition<5
#increment 1 by default
#range(1,6)
# start =1
# condition < 6
# increment 1 by default
#range(1,6,2)
# start = 1
# condition < 6
#increment 2
#range(condition)
for i in range(5):
print(i)
# range(initialization, condition)
for i in range(1,6):
print(i)