Posts

Python 3.4 - Math Module

Math module contains many math functions for simple numbers. If you want to work with complex number you need the cmath module. You can import the math module as normal, but you'll have to write math.whatever each time. TO make this simpler you can do (from math import *) and this will allow you to avoid writing math each time. But makes sure you don't write math anymore! https://docs.python.org/3/library/math.html https://www.youtube.com/watch?v=RquKAhdGajw

Python 3.4 - Coding Practice

The modulo operator: for i in range ( 10 ): print (i , i% 2 , ) if i% 2 == 0 : print ( 'no remainder' ) else : print ( 'you have a remainder' ) Assigning variables and basic math operators: x , r , g , z = ( 12 , 34 , 34 , 45 ) print (x + g) print (r + g + x) t = [ 1 , 2 , 3 , 4 , 5 ] print ( len (t)) t.append( 6 ) t.pop( 0 ) print (t) Classes contain functions and attributes for that class. You can assign a variable and/or function to a class. class Car: color = "blue" def drive ( self ): print ( 'i am driving now' ) def paint_car ( self , color): self .car_color = color newcar = Car() othercar = Car() print (newcar.color) print (othercar.color) newcar.paint_car( 'yellow' ) print (newcar.car_color) By using classes and assigning functions to the class.  You can then create an instance of that class and assign different attributes and functions to that instance. ...

Python 3.4 - Modulo

The modulo returns the remainder of division between two numbers. (x divided by y, x % y, 3 % 2) If the number dividing the first number is larger than it will return the first number.  https://www.youtube.com/watch?v=b5cb_nfDyyM

Pythonn 3.4 - Formatting Strings %s

The %s symbol is used to identify a string. In the example below the %s symbol will be replaced with the variable specified after the % (modulus). name = input ( "who are you?" ) print ( "hello %s" % (name , )) https://stackoverflow.com/questions/997797/what-does-s-mean-in-python

Python 3.4 - if __name__ == '__main__'

"When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__" . If this file is being imported from another module, __name__ will be set to the module's name. In the case of your script, let's assume that it's executing as the main function, e.g. you said something like python threading_example . py on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that __name__ does equal "__main__" , so it will execute the block s...

Python 3.4 - Fileinput Module

 Fileinput is a module that can access files and identify certain aspects of the files. https://docs.python.org/3/library/fileinput.html

Python 3.4 - String Methods

String Methods are built in to python 3.4. You don't need to import a module. String methods allow you to modify a string in many ways: https://docs.python.org/2.4/lib/string-methods.html strDummy = 'Knights who say ni' print (strDummy.lower()) strDummy = 'Knights who say ni' print (strDummy.replace( 's' , 't' )) This would split the string and get rid of the letter specified, but only once: strDummy = 'Knights who say ni' print (strDummy.split( 'n' , 1 )) ['K', 'ights who say ni'] https://www.youtube.com/watch?v=aJeb1j_dlOA