Data Types

Run Settings
LanguagePython
Language Version
Run Command
#int, float, complex, str, bool, list, tuple, set, dict #fundamental data types #int and float #print(type(2 + 4)) #--> print(type(6)) --> <class 'int'> #print(2 - 4) #print(2 * 3) #print(type(2 / 4)) #--> print(type(0.5)) --> <class 'float'> #print(2 % 2) #modulo operator --> gives remainder #print(type(0)) #print(type(20 + 1.01)) #--> print(type(21.01)) --> <class 'float'> #print(type(9.9 + 1.1)) #--> print(type(11.0)) --> <class 'float'> #print(2 ** 3) #--> print(2*2*2) --> 8 --> power function #print(2 // 4) #--> print(2/4=0.5) --> 0 --> rounds of to integer #print(5 // 4) #--> print(5/4=1.25) --> 1 --> rounds of to integer #--------------------------------------------------------------------- #math functions (built-in py fn.s) # print(round(3.1)) # print(abs(-20)) #--------------------------------------------------------------------- #operator precedence (priority list of operators) #() --> **// --> */ --> +- # print((20 - 3) + 2 ** 2) # print(bin(5)) # print(int('0b101',2)) #--------------------------------------------------------------------- #variables # iq1 = 206 # iq2 = iq1 * 1.05 # a = iq2 # print(a) # a,b,c = 1,2,3 # print(a,b,c) #constants # pi = 3.14 # print(pi) #statements and expressions # iq = 100 #here this whole line is a stmt # age = iq / 5 #here this whole line performing a certain task is stmt and iq / 5 is an expr #-------------------------------------------------------------------------------------------- #augmented assignment operator (+=, -=, *=, /=) # value = 5 # value += 3 # --> value + 3 --> 5 + 3 --> 8 # print(value) #-------------------------------------------------------------------------------------------- #str (strings) # print('hello!') # print(type('hello!')) # usr = 'stark' # pwd = '''iam # ironman''' # lstr = ''' # wow # 0 0 # | # ___ ''' # print(lstr) #a long string is defined by opening and closing it with 3 single inverted commas # name = 'Tony' # surname = 'Stark' # fullname = name + ' ' + surname # print(fullname) #print('hello' + '!') #string concatenation only works with strings not with int, float etc. # print(type(int(str(100)))) # a = str(100) # b = int(a) # c = type(b) # print(a,b,c) #------------------------------------------------------------------------------- #type conversions #escape sequence # weather = "\tIt's kind of sunny\" outside\n atb" #whatever comes after \ is a str # print(weather) #\t is for adding a tab #\n is for adding a new line #formatted strings # name = 'Steve' # age = 45 # print('hello ' + name + ' you are ' + str(age) + ' years old.') #normal str # print(f'hello {name} you are {age} years old.') #formatted str from py3 -- recommended # print('hello {} you are {} years old.'.format('Steve','45')) #dot format from py2 # print('hello {} you are {} years old.'.format(name,age)) #dot format from py2 # print('hello {1} you are {0} years old.'.format(name,age)) #dot format from py2 # print('hello {new} you are {old} years old.'.format(new = 'hey',old = '25')) #dot format from py2 #str indexes #selfish = '01234567' # 01234567 #[start : stop : stepover] # print(selfish[0 : 8 : 2]) #output: 0246 # print(selfish[1 :]) #output: 1234567 # print(selfish[: 5]) #output: 01234 # print(selfish[-1]) #output: 7 starts from end # print(selfish[-2]) #output: 6 starts from end # print(selfish[::-1]) #output: 76543210 starts from end # selfish = selfish + '8' # print(selfish) #string length fn. # greet = 'hello!' # print(len('hello!')) # print(len(greet)) # print(greet[0:4]) # print(greet[0:len(greet)]) #------------------------------------------------------------------------------- # quote = 'to be or not to be ?' # print(quote.upper()) #converts the whole into uprcase # print(quote.capitalize()) #converts the 1st letter into uprcase # print(quote.find('or')) #finds the 1st occurence of piece of text # print(quote.replace('or','ro')) #replaces the mentioned text # print(quote) #the upr line just creates a new modified copy and hence doesn't alter the original str #----------------------------------------------------------------------------------------------------- #booleans # name = 'Stark' # is_cool = False # is_cool = True # print(bool(1)) #since 1 is true it prints True # print(bool(0)) #since 0 is true it prints False # print(bool(is_cool)) #since var 'is_cool' is true it prints True # print(bool('')) #for any zero value it prints false # print(bool('steve')) #for any non-zero value it prints true #------------------------------------------------------------------------------- #lists (arrays of python) #Data Structure: Way to organize data in different efficient ways. # amz_cart = ['books', 'pens', 'pencils', 'toys', 'grapes'] # print(amz_cart[1]) # print(amz_cart[-1]) # print(amz_cart[0:3]) # amz_cart[0] = 'laptop' # new_cart = amz_cart # new_cart[0] = 'gum' # print(amz_cart[0::2]) # print(new_cart[0::2]) #------------------------------------------------------------------------------------- #matrix # matrix = [ # [1,5,1], # [0,1,0], # [1,0,1], # ] # print(matrix[0][1]) # matrix[0][1] = 0 # print(matrix[0][1]) # print(matrix) #------------------------------------------------------------------------------- #list methods #basket = [1,2,3,4,5] #print(len(basket)) #adding # new_list = basket.append(100) #only appends the list mentioned doesn't produce any result # basket.insert(4, 50) #format for insert: (index, element) and same as append only modifies list # print(basket) # print(new_list) # new_list = basket # print(new_list) # basket.extend([101,102]) # print(basket) # print(new_list) # #removing # basket.pop() #removes the last element (lifo) # print(basket) # basket.pop(0) #removes the first element # print(basket) # basket.remove(4) #removes the 4th element from the original list # print(basket) # poplist = basket.pop(4) #it returns the popped number remember only the pop returns value # print(poplist) # basket.clear() #completely clears the whole list # print(basket)
Editor Settings
Theme
Key bindings
Full width
Lines