Int-It Represents All Number Float - It Represents Numbers With Decimal Values STR - It Represents All Letters
Int-It Represents All Number Float - It Represents Numbers With Decimal Values STR - It Represents All Letters
<class 'int'> 5
<class 'int'> 45
<class 'float'>11.0
16
15625
40
2 it is remainder %
OPERATOR PRECEDENCE
#operator precdence
20 + 3 * 4 - here first 3 will be multiplied by 4 and then 20 will be added
#OPERATOR PRECDENCE
print (20 - 3 * 4) 8
print ((20 - 3) * 4) 68
print ((20 - 3) + 2**2) 21
# ()
# **
# * /
# + -
1. Complex numbers and binary representation of numbers, base number
is 2(base 2 numeral system 0s and 1s)
#complex - its a third type instead of int and float
print (bin(5)) #binary number for 5 is 101, and b in result means binary, it shows
its a binary number. 0b101- binary representation
0b101
0b1100
12
0b101100
44
VARIABLES
#VARIABLES
iq1 = 190
#here the iq of person is 190 and 190 is asigned to iq, so the variable here is iq.
print (iq1)
user_iq = 190 #variables must start with either lowercase or an _(underscore) and
they must be in snake case, which means instead of space we use underscore.
print(user_iq)
_user_iq = 190
print (_user_iq)
us4r_iq34 = 190
print (us4r_iq34)
#keywords should not be repeated, blue colored words are print words.example
print = 190
print (print)
#there will be a error if you use this
_____________________________________________________________________________________
iq = 190
user_age = iq/4
print (user_age) result = 47.5
#CONSTANTS
PI = 3.14
PI = 0
print (PI)
#we use PI in capital to shows that its not meant to be changed. and later we
reassigned the value of PI so it shows 0 in result instead of 3.14. we can reassign
any value.
#dunder variables starts from 2 underscores. and they should not be touched. we can
assign them value but dont practice with them
__hihi = 'hihi'
print (__hihi) result = hihi
#trick for assigning multiple values together to a variables
a,b,c = 1,2,3
print (a,b,c) result = 1,2,3
a,b,c = 1,2,'wow'
print (a) 1
print (b) 2
print (c) wow
iq = 100
user_age = iq/5
print (user_age, iq) 20.0 100
#here expression is iq/5, expression is code which produces value and statement is
the whole term, so we have two expression in above code. iq=100 and user......
_____________________________________________
some_value = 5
some_value = some_value + 5 #u can also do some_value = 5 + 2
print (some_value) 10
#augemented assignemt operator helps in doing this above thing more neatly
some_value = 5
some_value += 2 7
print (some_value)
some_value1 = 23
some_value1 *= 2 46
print (some_value1)
here the operator before the equal mark does the same thing as shown above, operator
must be on the left side of the equal sign
STRING – all words in python are called strings
#we can write strings through both marks, but there is a third way to write them too,
that is for long strings that is multiple lines
long_string = '''
WOW
O O
---
'''
print (long_string) WOW
O O
---
first_name = 'Surya'
last_name = 'Singh'
full_name = first_name + ' ' + last_name Surya Singh
print (full_name)
#up here u wont get space so we will add another string in full name with space or u
can put a space at the end of surya in first name or at the beggining of singh in
last name
#string concatenation only workd with string u cant add a number like 5 at the place
of surya
#TYPE CONVERSION
#how to make a string file read as a integer and vice versa.
#or
a = str(54)
b = int(a)
c = type(b)
print (c) <class 'int'>
# ESCAPE SEQUENCE
weather = "it\'s \"kind of\" sunny"
print (weather) it's "kind of" sunny
#here adding \ before ' and " means the coming sign would be used assumed to be a
string.
a = "its,\ suuny"
#FORMATTED STRINGS
print ('Hiii ' + name + '\nYou\'re ' + str(age) + ' years old')
#now to display this name johnny on profile page we can do it by formatted string
#now the above info lookjs complicated so we will write an f at the beginning of the
string. it will tell python its a formatted string.
_____________________________________________________________________________________
#formatted strings
Result –
Hii Surya Singh.
You're 19 years old.
selfish = '01234567'
#01234567
# [start:stop:stepover]
print (selfish[0]) #says start from zero
print (selfish[3]) #says start from 3 counting as 0,1,2,3
print (selfish[0:4]) #says start from 0 and and stop at 4. 4 is not readed.
print (selfish[0:8])
print (selfish[0:8:1]) #means start from 0 till 8 and jumover 1 number, for example
first zero, jumping 1 no, its 1 so read 1, then jump and read 2.
print (selfish[0:8:2]) #means start from 0 till 8 by jumoiing on 2 numbers, for
example first its 0, then jump 2 nnumber, it reaches 2 so read 2 then jump again 2
number it reaches 4, so read 4.....
print (selfish[1:]) #it says start from 1 and till deafault so means all number
print (selfish[:5]) #means start at default 0 all the way to 5. 5 is not read
print (selfish[-1]) #in pyhton negative index means start at the end.
print (selfish[::-1]) #it means start at default stop at default and stepover from
back. it gives us reverse.
print (selfish[::3])
print (selfish[::-2])
RESULTS – 0
3
0123
01234567
01234567
0246
1234567
01234
7
76543210
036
7531
#IMMUTABILITY
#a value in string cannott be reassigned, you have to assign it completely
name = 'Surya'
name = 'Ayushi'
print (name)
#here we completely assigned a new value, but what we if we assign a new value to a
part of string, lets see
marks = '214791'
marks[2] = '6'
print (name + marks)
#here we get an error saying string does not support item assignment, if you want to
change the value then u need to completely reassign it.
income = 3456
income[3] = 9
print (income)
#so integer also does not support item assignment
Results - Ayushi
Traceback (most recent call last):
File "main.py", line 10, in <module>
marks[2] = '6'
TypeError: 'str' object does not support item assignment
#BUILT IN FUNCTION
#1 – len
greet = 'hellloooo'
#PASSWORD CHECKER
#exercise on password checker
password_length = len(password)
hidden_password = '*' * password_length
print(f'{username}, your password, {hidden_password} is {password_length} letters
long.')
LIST
LIST SLICING
amazon_cart = ['notebook',
'sunglasses',
'pen',
'toys',
'grapes',
]
amazon_cart[0] = 'laptop'
new_cart = amazon_cart[0:3]
new_cart[0] = 'gum'
print (new_cart)
print (amazon_cart)
#NOTE - List is immutable, here above new cart has created a copy of amazon cart so
amazon cart is not changed but if you remove the slicing braackets [] then it will
overwrite. to completely copy amazon cart without any changes use this [:] as shown
below
walmart_cart = [
'cheese',
'brownies',
'ice cream',
'chips'
]
walmart_cart[0] = 'ghee'
walmart_side_cart = walmart_cart[:]
walmart_side_cart[0] = 'coconut oil'
print (walmart_side_cart)
print(walmart_cart)
Results-
matrix = [
[1,5,1],
[0,1,0],
[1,0,1]
]
print (matrix[0][1])
#here we wanted to access the first item of whole array and the that array's 2nd
item.
basket = [1,2,3,4,5]
print (len(basket))
#here append 100 just adds 100 but it does not produce result it just changes the
list.
#insert - to insert sth, we need to first write index where we want to insert then
the object
list_2 = [1,3,5,7]
list_2.insert(4, 9) #here 4 is 7 so 9 will be added in place of 7 and will come on
place of 4 and 7 will move to spot 5
list_2.insert(0, 2) #here 2 will be on place zero, so 1 will move to place 1.
list_2.insert(1, 100)
list_2.insert(3, 99)
print(list_2)
result - 5
[1, 2, 3, 4, 5, 6, 100]
[2, 100, 1, 99, 3, 5, 7, 9]
basket = [1,3,5,7,9]
new_list = basket.extend([100,101])
basket.pop()
basket.pop(0) #o will remove whatever is on place 0 or index
print(basket)
print(new_list)
#pop needs index of the data which u want to remove like 0,1,2,3 etc. but remove
needs the value, you put the value and it wil be removed.
#extend does not return anything so does remove, but pop returns thr value it popped
off.
basket_2 = [13,14,23,53,654,765,8]
basket_2.clear()
print (basket_2)
Result =
[3, 5, 7, 9, 100]
[3, 5, 7, 9, 100]
None
[1, 5, 6, 7, 8, 11]
[]
basket = ['a','b','x','c','d','e','e','a']
print(basket.index('d', 0, 5))
print(basket.index('b', 0 ,4))
print('a' in basket)
print('f' in basket)
print('i' in 'hii my name is adrien')
print(basket.count('d'))
print(basket.count('e'))
print(sorted(basket))
print(basket)
#sorted means this in sort form
new_basket = basket[:] #or
new_basket_1 = basket.copy()
new_basket_1.sort()
print(new_basket_1)
new_basket.sort()
print(new_basket)
basket.sort()
print(basket)
basket.reverse()
print(basket)
RESULT –
4
1
True
False
True
1
2
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['a', 'b', 'x', 'c', 'd', 'e', 'e', 'a']
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['x', 'e', 'e', 'd', 'c', 'b', 'a', 'a']
sentence = ''
new_sentence = sentence.join(['hi ', 'my ', 'name ', 'is ', 'JOJO.'])
print(new_sentence)
#range helps us to create a numbered list real quick.
#.join works on string, an empty string
Result –
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
hi my name is JOJO.
#list unpacking
a,b,c,*other,d = [1, 2, 3, 4, 5 , 6, 7, 8, 9]
print(a)
print(b)
print(c)
print(other)
print(d)
Result –
1
2
3
[4, 5, 6, 7, 8]
9
#Dictionary - python calls it dict- its a data type but also data structure,
curly brackets denotes dictionary, it has a key and a value
dictionary = {
'a' : 1,
'b' : 'Hellooo',
'd' : [1,2,3],
'c' : True
}
my_list = [{
'a' : [1,2,3],
'b' : 'Hiiii',
'c' : 4,
'd' : True
},
{
'a' : [5,6,8],
'b' : 'Heyyyo',
'c' : True,
'd' : 5
}]
print(dictionary['b'])
print(dictionary)
print(my_list[1]['a'][1])
Result –
Hellooo
{'a': 1, 'b': 'Hellooo', 'd': [1, 2, 3], 'c': True}
6
}
print (user['123'])
print(user[23])
print(user['age'])
print(user.get('weight', 23))
Result –
[1, 2, 3, 4]
43
34
23
{'name': 'Ayushi'}
user = {
'name' : 'Ayushi',
'basket_items' : ['Grapes', 'Apples', 'Biscuits'],
'age': 19,
'weight' : 45
}
print(user.keys())
print(user.items())
print('age' in user.keys()) #checking keys
print( 19 in user.values()) #checking value
print(user.pop('name'))
print(user) #pop changes the actual list
print(user.popitem()) #it randomly pops of any key and value, in this case its weight
print(user.update({'age': 20}))
user.update({'skinny_level': 10})
#user.update either updates the existing key's value or creats a new key and valu if
the same do not exist.
user_2 = user.copy()
user.clear()
print(user_2)
print(user)
RESULT –