01 Python Notes
01 Python Notes
print("Hello World")
> Variables
Example 1:
----------
name = "shradha"
age = 22
print(name)
print(age)
> Comments
Example 2:
----------
Example 3:
----------
# Type Conversion
1. float()
2. bool()
3. str()
Python-2
4. int()
Example 4:
----------
# Sum of 2 Numbers
Example 5:
----------
# Strings
print(name.lower())
print(name)
print(name.find('y'))
print(name.find('Y'))
print(name.find("Stark"))
print(name.find("stark"))
print("Stark" in name)
print("S" in name)
print("s" in name)
Output
------
TONY STARK
Tony Stark
tony stark
Tony Stark
3
-1
5
-1
Python-3
Ironman
Tony Stark
True
True
False
print(5 + 2)
print(5 - 2)
print(5 * 2)
print(5 / 2)
print( 5 // 2)
print(5 % 2)
print(5 ** 2)
Output
------
7
3
10
2.5
2
1
25
number = 2
Python-4
print(number > 3)
print(number < 3)
print(not number > 3)
print(not number < 3)
print(number > 3 and number > 1)
print(number > 3 or number > 1)
# if statement
age = 13
if age >= 18:
print("you are an adult")
print("you can vote")
elif age < 3:
print("you are a child")
else:
print("you are in school")
print("thank you")
Example
--------
# Simple Calculator
if operator == "+":
print(first + second)
elif operator == "-":
print(first - second)
elif operator == "*":
print(first * second)
elif operator == "/":
print(first / second)
elif operator == "%":
print(first % second)
else:
print("Invalid Operation")
------------
for loop
------------
Example
--------
for i in range(5):
print(i)
Output
------
0
1
2
3
4
Example
--------
for i in range(2,5):
print(i)
Output
------
2
3
4
Example
--------
for i in range(2,5,2):
print(i)
Output
------
2
4
Python-7
while Loop
------------
Example
--------
i = 1
while(i <= 5):
print(i)
i = i + 1
Output
------
1
2
3
4
5
Example
-------
i = 1
while(i <= 5):
print(i * "*")
i = i + 1
Output
-------
*
**
***
****
*****
> List
-------
Example
--------
friends[0] = "aman"
print(friends)
print(friends[0:2]) #returns a new list
print("-------------------------------")
Output
------
amar
akbar
anthony
akbar
-------------------------------
['aman', 'akbar', 'anthony']
['aman', 'akbar']
-------------------------------
aman
akbar
anthony
--------
Example
--------
marks.insert(0, "math")
marks.insert(1, 99)
print(marks)
print("-----------------------------------")
print("math" in marks)
print("------------------------------------")
i = 0
while i < len(marks):
print(marks[i])
print(marks[i+1])
i = i + 2
Python-9
print("------------------------------------")
print(len(marks)/2)
marks.clear()
print(marks)
Output
-------
Example
-------
print("---------------------")
Output
-------
ram
shyam
kishan
---------------------
ram
shyam
radha
radhika
> Tuples
---------
They are like lists (sequence of objects) but they are immutable
i.e. once they have been defined we cannot change them.
Parenthesis in tuples are optional.
Example
--------
Output
------
2
2
Sets
-----
Sets are a collection of all unique elements.
Indexing is not supported in sets.
Example
-------
Output
-------
{97, 98, 95}
97
98
95
> Dictionary
-------------
Example
--------
marks["english"] = 95
print(marks)
marks["math"] = 96
print(marks)
Output
------
a. In-built functions
----------------------
b. Module functions
--------------------
import math
print(dir(math))
print("---------------------------------------")
import random
print(dir(random))
print("---------------------------------------")
import string
print(dir(string))
print("---------------------------------------")
-----------------------------
c. User-defined functions
-----------------------------
Example
--------
sum(1, 2)
sum(1)
Python-13
Output
------
3
5