Fundamental Data Types - int/float and variables

Run Settings
LanguagePython
Language Version
Run Command
#Fundamental Data types int float bool str list tuple set dict #Classes -> custom types #Specialised Data Types - extra modules from libraries. Extensions effectively None #none is simply nothing, such as 0 in maths #Learning Int and Float int # integer/number e.g. 3, 4, 5 etc. used for mathematical operations. Includes positive and negative numbers # print(2 + 4) #print is effectively saying perform this action and output it # print(type(2 + 4)) #type outputs what data type this is. Functions work from within brackets # #for example, here it would do 2+4 = 6, then do type of 6, then print the type # print(type(2 / 4)) #as 2/4 = 0.5, the answer is 0.5 and the type is therefore float, rather than int float #floating point number. Alternatively, any number with a decimal point e.g. 0.5 # print(type(5.00001)) #type will be float due to the decimal place # #floating point stores the number in two different locations for each side of the decimal - more memory is needed # print(type(20+1.1)) #here python automatically adjusts the type to float, as the int + float creates a float # print(type(9.9+1.1)) # despite the two floats being added into a single number, it maintains float as a type - 11.0 instead of 11 # print(2 ** 2) #double multiplication symbol = to the power of, e.g 2 to the power of 2 here # print(2 // 4) #double division returns an integer rounded down # print(5 % 4) #% = modulo, providing the remainder of the division e.g. 5/4 = 4 remainder 1, 1 is output # print(6 % 4) # here 6/4 = 1 remainder 2, 2 is output #Math Functions # print(round(3.9)) # rounds to nearest whole # print(abs(-20)) #returns absolute value of argument - simply removes negative e.g. -20 outputs 20 # # there are many math functions available but generally context dependent and used as and when needed - no need to learn all, just find them # #search - Python3 math functions - programiz #Operator Precedence # print(20 - 3 * 4) #follows standard rules for mathematics - in this case multiplication of 3*4 happens first, then 20-12 #Order of precedence: # () - brackets # ** - power of # * / - mult/div # + - - plus/minus # print((5 + 4) * 10 / 2) # 45 # print(((5 + 4) * 10) / 2) # 45 # print((5 + 4) * (10 / 2)) # 45 # print(5 + (4 * 10) / 2) # 25 # print(5 + 4 * 10 // 2) # 25 # it would appear any time there is a division towards end it provides a float, unless // used which provides an integer # Complex exists for highly complex equations - not super relevant # bin for showing the binary version of a number # print(bin(5)) #prints 0b101 - 0b is what python uses to denote as a binary number, 101 is the actual bin ver of 5 # print(int('0b101' , 2)) # returns 5 from the binary number, base 2 # Variables # iq = 190 # iq is the variable, 190 is the data for it. Now iq will represent 190 everywhere in program # print(iq) # will print the value of iq, here being 190 # # variables are snake_case i.e. all lower case, no spaces just underscores. # # must start with lowercase or underscore, can use letters/numbers/underscores, are case sensitive, and don't overwrite keywords # _user_iq = 190 # print(_user_iq) # matches above and will print, but would not if it differed even by case # #python keywords w3schools to see what all key words are - these cannot be used for variables # #make variables strongly descriptive for ease of understanding # user_age = iq/4 #variables can be used in operations for other variables, in this example iq which is 190 will be /4 # print(user_age) #constants # PI = 3.14 # using caps denotes a constant to not change # #don't use __ as these are also used for other constants # a,b,c = 1,2,3 # quick way to assign multiple variables at once # print(a) # print(b) # print(c)
Editor Settings
Theme
Key bindings
Full width
Lines