0% found this document useful (0 votes)
16 views

Python Notes

The document discusses Python concepts like print statements, variables, data types, operators, conditional statements and loops. Print statements are used to display output and variables are used to store values. The main data types covered are numeric, string, list, tuple, dictionary and set. Operators like arithmetic, comparison, logical and bitwise operators are also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Notes

The document discusses Python concepts like print statements, variables, data types, operators, conditional statements and loops. Print statements are used to display output and variables are used to store values. The main data types covered are numeric, string, list, tuple, dictionary and set. Operators like arithmetic, comparison, logical and bitwise operators are also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PYTHON NOTES

print() : It is an inbuilt function. We use this function to print or display


any thin which we want to display.
Like if we want to print any string then we need to write under “here” and
if we want print any number we can directly write and print. Eg:
print(“Hello_World”), print(3)
o/p :- Hello_World, o/p:- 3
Variable : It is a name in which we store a value.
>>Variable ka naam kisi bhi number se start nhi hona chahiye.
>>Variable ke naam bich kabhi bhi space nhi hona chahiye.
>>Variable kabhi bhi special character or comma dash plus etc se start nhi
hona chahiye except _ (under score).

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.

Operator: There are various operator like arithmetic operator, Assignment


operator, Comparison Operator, Logical operator, Identify Operator,
Membership Operator, Bitwise operator.
Arithmetic Operator: - When we do mathematical work then we use
arithmetic operator. Below are the Arithmetic Operator
Operator Name Example
+ Addition X+Y
- Subtraction X-Y
* Multiplication X*Y
/ Division X/Y
% Modulus X%Y
** Exponents X**Y
// Floor Division X//Y

Assignment Operator: - it is use to assign any value into any variable.


Below are the assignment operator:
Operator Example Same As
= X=5 X=5
+= X+=3 X=X+3
-= X-=3 X=X-3

Comparison Operator: - It is use to compare 2 value and provide the us


that this is true or false. Below are the comparison operator:
Operator Name Example
== Equal X==Y
!= Not equal X!=Y
> Greater Than X>Y
< Less Than X<Y
>= Greater than or Equal X>=Y
to
<= Less than or Equal to X<=Y

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

Data type in python: -


Below are the data types in python :
 Numeric
1. Integers
2. Float
3. Complex Numbers
 Sequence Type
1. String
2. List
3. Tuple
 Dictionary
 Set
Python categorized data type into two part.
Mutable data type
Immutable data type
 Mutable object can change its state or contents and immutable objects
cannot.
Mutable Data Type:
List
Dictionary
byte array
Immutable Data Type:
Int
Float
Complex
String
Tuple
set
Number:
o Integers
o Float
o Complex numbers
a=5 print(a,”is of type”,type(a)) #5 is of type <class ‘int’>
a=2.0 print(a, “is of type”,type(a)) #2.0 is of type<class’float’>
a=1+2j print(a, is of type”,type(a)) # 1+2j is of type<class’complex’>

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 want to take integer type value:


A=int(input(“Enter the value”))
Print(A)

If we want to take float type value:


A=float(input(“Enter the value”))

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

If statement: it is used when we have single condition:


Syntax: if [conditional expression]:
[statement(s) to execute]
Eg. A=10
If a%2==0:
print(“Even Number”);
IF else Statement: it is used when we have 2 statement
Syntax: if [conditional expression]:
[statement(s) to execute]
else:
[statement(s) to execute]

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”)

for loop with range():

#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)

# range(initialization, condition, increment)


for i in range(1,6,2):
print(i)

You might also like