Data Types

Run Settings
LanguagePython
Language Version
Run Command
#Fundamental Data Types #int, float, str, bool, dict, tuple, list set #Classess --> Custom types #Specialized Data Types --> Modules, parkages # Fubdamental Data Types -- int and float #print(2 + 4) #print(2 - 4) #print(2 * 4) #print(2 / 4) #Getting the Data Types #print(type(2 + 4)) #print(type(2 - 4)) #print(type(2 * 4)) #print(type(2 / 4)) # 0.5 print(type(20 +1.1)) print(9.9 +1.1) print(type(9.9 +1.1)) #OPerators in python #print(2 ** 2) # 2 raised power of 2 will give 8 #print(5 // 4) # double division round down the result , 1.25 --> 1 #print(6 % 4) # mod of, will get the reminder 2 #Math functions #print(round(3.9)) #4 #print(abs(-20)) #20 #Dictionary data type, order does not matter, key value pair dict1 = {'123': [1,2,3], 'True': 'hello', 'Name': 'John', 'ID' : 4321 } print(dict1['ID']) user = {'age': 45, 'height': '6.2', 'bmi': 18, 'pin': '001' } print(user.get('weight')) print(user.get('weight', 40)) # incase weight does not exit, use default value 40 #using the dict function to create dict on the fly user2 = dict(name = 'Jeriah') print(user2) #How to look for item in dict print('height' in user) #useing dict methods to fetch things from our dict #print('height' in user.keys()) #print(18 in user.values()) #print(user.items()) #user3 = user.copy() #copy the items to another dict #print(user.clear()) #clear the entire dict, renturning none #print(user3) #print(user3.pop('pin')) #removes the last value from the dict #print(user3) #print(user3.popitem()) #radomly removes items from the dict #print(user3) #print(user.update({'age': 30})) #print(user) #Tuple data types my_tuple = (1,2,3,4,5) # Tuples are imuitable, once created, they cannot be change, the faster than a list nariah = ('Nanna', 3, 17.5, 'Zambian-Ghanian-American') nariah1 = ('Nanna', 3, 17.5, 17.5,17.5,'Zambian-Ghanian-American') #Accessing item in a tuple'Zambian-Ghanian-American' print(5 in my_tuple) print(my_tuple[4]) print(my_tuple[3]) print(my_tuple[2]) print(my_tuple[1]) print(my_tuple[0]) print(nariah[2]) print(len(nariah)) #Slicing a tuple Ama = nariah[1:2] #tuple Methods print(nariah1.count(17.5)) x = nariah1.index(3) print(x) print(nariah1.index(17.5)) print(nariah1.index('Zambian-Ghanian-American')) #Using built in fun length to get length of a tuple print(len(nariah1)) #list data types -- List is an order sequence of objects, denoted by [], list are like form of array li = [1,2,3,4,5] li2 = ['a', 'b', 'c'] li3 = [1,2.5,'a', True] print(li) print(li2) print(li3) #Accessing items in a list amazon_cart = ['notebooks', 'sunglasses', 'ipads'] print(amazon_cart[2]) #list slicing amazon_cartz = ['notebooks', 'sunglasses', 'ipads', 'toys', 'peanuts', 'jams', 'powder milk' ] print(amazon_cartz[0:2]) print(amazon_cartz[0::2]) amazon_cartz[6] = 'butter' print(amazon_cartz) print(amazon_cartz[::-1]) print(amazon_cartz[::-4]) print(amazon_cartz[::-2]) new_cart = amazon_cart[:] print(new_cart) new_cart[0] = 'gum' print(new_cart) #str data types print(type('hi hello there')) print('hi hello there') username = 'supercoder' password = 'supersecret' long_string = ''' wow o o --- ''' print(long_string) first_name = 'Jeriah' last_name = ' Smarte' full_name = first_name + last_name print(full_name) #string concatenation print('Hello' + ' Jeriah!') #Type conversion print(type(str(100))) #converting 100 to string print(type(int(str(100)))) #convert print(type(float(str(100)))) #converting string to double a = str(100) b = int(a) c = type(b) print(c) #exercise-- Type Conversion #birth_year = input('What year were born?') #age5 = 2019 - int(birth_year) #print(f'your age is: {age5}') birth_year = 2000 #int(input('What year were born?')) age5 = 2019 - birth_year print(f'your age is: {age5}') #Ecape Sequence weather = 'It\'s sunny' print(weather) weather1 = "It\\s \"kind of\" sunny" print(weather1) weather2 = "\t It\'s \"kind of\" sunny" print(weather2) weather3 = "\t It\'s \"kind of\" sunny\n hope you have a good day!" print(weather3) #Formatted strings name = 'Nanna' age = 3 name1 = "Ama" age1 = 4 name2 = 'Jeriah' age2 = 1 print('Hello ' + name + ', you are ' + str(age) + ' years old.') #making our code dynamic print(f'Hello {name1}, you are {age1} years old.') print('Hello {}, you are {} year old.'.format('Jeriah', '1')) #the .format method name3 = 'Maria' age3 = 38 print('Welcome {0}, you truned {1} on your birthday!'.format(name3, age3)) #Indexes of strings, the rules [start:stop:stepover] (string slicing), this is us to return parts or all the strings selfish = 'me na gic' #012345678 yankee = 'United States' print(selfish[6]) #return string position #6 -->g print(selfish[0:8]) #retrun the string me na gi[0:8] print(selfish[0:9]) #retrun the string me na gic [0:9] print(selfish[0:9:2]) #retrun the string me na gic [0:9:1] print(selfish[1:]) #start at 1, no stop,it will default and return from 1 position all the way to the end of string print(selfish[:9]) #retrun the string me na gic using [:9] no start only end, will return all the strin print(selfish[:5]) print(yankee[::1]) #double colons, start at zero with 1st colon, 2nd colon ends when the string ends, stepover by 1 print(yankee[-1]) #In Python -1 index means start counting from the end of the string (eg -->13. 0,1,2,3,4,5,6,7,8,9,10,11,12,13) print(yankee[-2]) print(yankee[::-1]) #This will return the revese of the string --> setatS detinU print(yankee[::-2]) print(len(yankee)) #Immutability of strings, part of a string can not be assign dog = 'bull dog' #0123456789 #dog[0] = '2' dog = '2' #the whole variable needs to be resigned just get 2 print(dog) #Exploring more on Built-In Functions and Methods greet = 'helloooo' print(len(greet)) print(greet[::1]) print(greet[0:len(greet)]) #methods quote = 'to be or not to be' print(quote.capitalize()) # capitalize the first occurance of the sentence print(quote.find('be')) print(quote.replace('be', 'me')) #Booleans Datatype -- represent the idea of true and false homosapien = 'Smarte' is_cool = False is_cool = True print(is_cool) print(bool(1)) #In programing bool is just 0 and 1, 0 = false, 1 = true print(bool(0)) print(bool('True')) #set data types - unordered collection of unique objects, no duplicate my_set = {1,2,3,4,5,5} my_set1 = {1,2,3,4,5,5,5} #{1, 2, 3, 4, 5}, no douplicates my_set.add(100) my_list1 = [1,2,3,4,5,5] #how do return a unique list print(set(my_list1)) #using the set function to return unique set from a list print(my_set) print(my_set1) #accessing elements of a set my_set = {1,2,3,4,5,5} #print(my_set[0]) print(1 in my_set) print(len(my_set)) print(list(my_set)) #converting a set to list new_set = my_set.copy() #copying a set to a new set print(new_set) new_set.clear() print(new_set) #set methods #.difference(), .discard(), .difference_update(), .intersection(), .isdisjoint(), #.issubset(), .issuperset(), .union() nanna_set = {1,2,3,4,5} ama_set = {4,5,6,7,8,9,10} #Password Checker #username = input('jariah') #password = input('secret') #print(f'{username} your password {password} is {len(password)} letters long') #Password Checker #username = input('What is your username?') #password = input('What is your password?') #password_length = len(password) #hidden_password = '*' * password_length #print(f'{username}, your password, {hidden_password}, is {password_length} letters long') #Matrix --it is way to describe two-diamensional list, it is an array with nested array matrix = [ [1,2,3], [4,5,6], [7,8,9] ] print(matrix[0][1]) #the result should be 2 print(matrix[1][1]) #the result should be 5 print(matrix[2][2]) #the result should be 9
Editor Settings
Theme
Key bindings
Full width
Lines