In [2]:
input_str = input("enter string:")
print(input_str)
print(type(input_str)) # str
hello <class 'str'>
bool¶
In [19]:
is_male = True
print(type(is_male))
<class 'bool'>
NoneType¶
In [20]:
empty_value = None
print(type(empty_value))
<class 'NoneType'>
integer input¶
In [1]:
input_int= int(input("enter num:"))
print(input_int)
print(type(input_int)) # int
5 <class 'int'>
array input¶
In [10]:
input_list_str = input("enter list with spaces:")
print(input_list_str)
input_list_str_array = input_list_str.split(" ")
print(input_list_str_array)
input_list_map = map(int,input_list_str_array)
input_list = list(input_list_map)
print(input_list)
1 2 3 ['1', '2', '3'] [1, 2, 3]
In [11]:
age = 25
print(type(age))
<class 'int'>
float¶
In [12]:
height = 5.9
print(type(height))
<class 'float'>
str¶
In [13]:
name = "piyush"
print(type(name))
<class 'str'>
list¶
In [14]:
fruits = ["apple", "mango","banana"]
print(type(fruits))
<class 'list'>
tuple¶
In [15]:
coordinates = (10,20)
print(type(coordinates))
<class 'tuple'>
dict¶
In [17]:
person = {
"name":"Piyush",
"age":30,
"height" :6.1,
"languages":["english","hindi"]
}
print(type(person))
<class 'dict'>
set¶
In [18]:
unique_numbers = {1,2,3,4,5}
print(type(unique_numbers))
<class 'set'>
If Else Statements¶
In [22]:
marks = 55
if marks < 40:
print("F")
elif marks >=40 and marks < 60:
print("C")
elif marks >=60 and marks < 80:
print("B")
elif marks >=80 and marks < 90:
print("A")
else:
print("A+")
C
Switch Statements¶
In [23]:
day =4
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
case _:
print("invalid day")
Thursday
In [24]:
num = 5
for i in range(1,11):
print(f"{num} x {i} = {num * i}")
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
2D Iteration¶
In [25]:
tables = [2,3]
for num in tables:
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
print("")
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30
While Loops¶
In [26]:
my_list = [1,2,3,4,5,6,7,9]
target = 7
idx =0
while idx < len(my_list):
num = my_list[idx]
if num == target:
print(f"{target} found at index {idx}")
idx +=1
print(f"{target} not found")
7 found at index 6 7 not found
Functions¶
Pass by value¶
- int, float , bool , str
In [28]:
def modifyVal(age,name,height,is_male):
age = 0
name = ""
height = 0.0
is_male =False
age = 31
name = "piyush"
height = 6.1
is_male = True
modifyVal(age,name,height,is_male)
print(age,name,height,is_male)
31 piyush 6.1 True
Pass by reference¶
- dict, list ,
In [29]:
def modifyVal(person):
person["age"] = 0
person["name"] =""
person["height"] = 0.0
person["is_male"] = False
person = {
"name": "Piyush",
"age":31,
"height":6.1,
"is_male" :True
}
modifyVal(person)
print(person)
{'name': '', 'age': 0, 'height': 0.0, 'is_male': False}
Mathematical Operations¶
In [8]:
import math
print(math.log(1000,10))
print(int(math.log(1000,10)))
print(math.pow(10,3))
print(10 ** 3)
print(abs(-100))
print(math.floor(100.7) )
print(math.ceil(100.7) )
2.9999999999999996 2 1000.0 1000 100 100 101
Strings¶
Modify the existing string¶
str is immutable
- first convert into list
- then update the value
- convert the list into str
In [9]:
s = "hello"
s_list = list(s)
s_list[0] = "H"
s = "".join(s_list)
s += "I"
print(s)
HelloI
Accessing index and char in a single iteration¶
In [5]:
s = "hello"
for i , ch in enumerate(s):
print("i",i)
print("ch",ch)
i 0 ch h i 1 ch e i 2 ch l i 3 ch l i 4 ch o
Splitting the strings into words by spaces¶
It handles multiple spaces too
In [8]:
print("piyush is best".split())
print(" preeti is very good ".split())
['piyush', 'is', 'best'] ['preeti', 'is', 'very', 'good']
Classes¶
Defining a class¶
In [3]:
class Person:
def __init__(self,name):
self.name = name
self.age = None
def set_age(self,age):
self.age = age
person = Person("piyush")
print(person.name)
print(person.age)
person.set_age(31)
print(person.age)
piyush None 31