|
| 1 | +import math |
| 2 | +import random |
| 3 | +from decimal import Decimal # when we deal with decimal point we import it to get more precise results |
| 4 | +persentage = int(2.99992) # int will convert it into integer |
| 5 | +print(persentage) |
| 6 | +marks = float(2) # it will convert into flooting points |
| 7 | +print(marks) |
| 8 | +item = 20 |
| 9 | +print (marks + 100 , persentage+3 , item) # we can also get multiple variable values like this |
| 10 | +# Arithematic operators |
| 11 | +print ( item / marks) # for division |
| 12 | +print ( item % marks) # for modulous mean remaing |
| 13 | +print ( item * marks) # for multiplication |
| 14 | +print ( item + marks) # for addition |
| 15 | +print ( 25 ** 19 ) # for power |
| 16 | +print('floor Division' , 15 // 2) # it will give the floor value of any divion value mean without decimal value |
| 17 | +# comparission operators |
| 18 | +print(2>5) # 2 is not greater then 5 then it will be false |
| 19 | +print(2<5) # as 2 is less then 5 then it will be true |
| 20 | +print(2 == 5 ) # as 2 is not equal to 5 then it will also be a false |
| 21 | +print(2 >= 5 ) # greater then or equal then if one of then is true then it will be true |
| 22 | +print(2 <= 5 ) # less then or equal then if one of them is true then it will be true |
| 23 | +print(2 != 5 ) # is not equal to mean to check it is not equal to yes 2 is not equal to |
| 24 | +# Logical operators |
| 25 | +print( 2 == 2 and 5 ==5 ) |
| 26 | +number_one = 1 |
| 27 | +number_two = 2 |
| 28 | +print( number_one > 4 and number_two < 10 ) # and mean if all are true then it will be true and if one of them is false then the whole will pe false |
| 29 | +print ( number_one > 10 or number_two > 1 ) # if one of them is true then it will be true |
| 30 | +print( not(number_one == number_two) ) # not will convert the true into false and false into true mean it will reverse any result |
| 31 | +# Assignment operators |
| 32 | +x = 20 |
| 33 | +x += 1 # it will be seems like X = X + 1 but mostly expert programmer use it as X += 1 |
| 34 | +x -= 1 # for substraction |
| 35 | +x *= 2 # for multiplication and to assignment to that one value |
| 36 | +x /= 2 |
| 37 | +x //= 2 |
| 38 | + |
| 39 | +print(x) |
| 40 | +# Identity operator are use to compare the memory location of two objects . they check the whether two variable refers to the same object in the memory |
| 41 | +t = [1,2,3,4,5] |
| 42 | +s = t |
| 43 | +print( s is t ) # is will return true if both variables refers to the same object in the memory |
| 44 | +print(id(s)) |
| 45 | +print(id(t)) # both id's are same mean both are the same object |
| 46 | +t = [1,6,3,0,5] |
| 47 | +print(s is t) # it is false because t is now a new object seprate object in the memory |
| 48 | +k = [1,6,3,0,5] |
| 49 | +print(id(k)) # id will show the object id in the memory |
| 50 | +print(id(t)) |
| 51 | +# string |
| 52 | +name = 'ali' |
| 53 | +second_name = 'ali' |
| 54 | +# name = 'haider' # If multiple variables are assigned the same value (especially of an immutable type like small integers or simple strings), they may refer to the same object in memory. But if any of those variables is later assigned a different value, Python will create a new object in memory for that variable. |
| 55 | +print(name is second_name) |
| 56 | +boss = [1,2,3,4] |
| 57 | +ok_boss = [1,2,3,4] |
| 58 | +print(boss is ok_boss) # it is giving false becase the seprate object list is created in the memory |
| 59 | +boss[0] = 55 |
| 60 | +print(ok_boss is boss) # it is giving as false value because the value of boss list is changed but the ok_boss still have old one |
| 61 | +print(math.floor(3.9)) # floor will give the lower bound value output 3 |
| 62 | +print(math.floor(-3.9)) # lower value will be -4 |
| 63 | +print(math.trunc(3.9)) # it will goes the value towords Zero 0 like in x and y axes it will go to 0 |
| 64 | +print(math.trunc(-3.9)) # the closest value towords zero is -3 |
| 65 | +print(int('64', 8)) # converted into octal |
| 66 | +print(int('64' , 16)) # it is also converted into hexa |
| 67 | +print( int('10000' , 2)) # it is converted into binary |
| 68 | +# random |
| 69 | +print(random.randint(0,100)) # it will give us random integer according to the range that we have given to it |
| 70 | +juice = ['apple juice', 'mango juice' , 'banana juice', 'orange juice'] |
| 71 | +print(random.choice(juice)) # it will get random choice that we will provide it it will pick randomlly |
| 72 | +random.shuffle(juice) |
| 73 | +print(juice) |
| 74 | +print(0.1+0.1+0.1-0.3) # this is giving us wrong results that's why we import decimal so library so we can get more accorate results |
| 75 | +print( Decimal("0.1") + Decimal("0.1") - Decimal('0.2') ) # now it is our accprate result by using the decimal library |
| 76 | +# sets |
| 77 | +set_one = {1,3,7,8} |
| 78 | +set_two = {2,4,5,6,9,0} |
| 79 | +print(set_two | set_one ) # this will give us Union all the elements of set into an order |
| 80 | +print( set_one & {1,7} ) # this will give us intersection mean common elements of array |
| 81 | +print( set_one - {1,3,7,8}) # it is giving emepty SET() mean nothing the set have it will never ever give empty {} because it is use for only dictionary |
| 82 | + |
0 commit comments