Last active
August 7, 2017 18:35
-
-
Save reinisriekstins/d2cca75e73bfb64b335efeca451b88d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # single line comment | |
| ''' | |
| multi | |
| line | |
| comment | |
| ''' | |
| # strings | |
| print('Hi there') | |
| print("You're awesome") | |
| # concatenation | |
| print('What\'s ' + 'your name?') | |
| print('Mine','is','Johnny') | |
| print('High',5) | |
| # string/ number conversion | |
| print('High ' + str(10)) | |
| print(str(2) + str(3)) | |
| print(float('2.7') + int('3')) | |
| # math | |
| # operators: + - * / ** | |
| print(5/2) | |
| print(5**2) | |
| # variables | |
| myVar = 42 | |
| print(myVar) | |
| printVar = print('whoa') | |
| printVar | |
| x,y = (3,5) | |
| print(x) | |
| print(y) | |
| # loops | |
| condition = 1 | |
| while condition < 10: | |
| print('Condition',condition) | |
| condition += 1 | |
| # while True: | |
| # print('infinite loop') | |
| forList = [1,3,7,3,4,97,324,654,34,12] | |
| for item in forList: | |
| print('forList item', item) | |
| for x in range(1,11): | |
| print('x is', x) | |
| # statements and comparison | |
| xx = 10 | |
| yy = 11 | |
| zz = 10 | |
| if xx > yy: | |
| print('xx > yy') | |
| elif xx < yy > zz: | |
| print('xx < yy > zz') | |
| if xx <= zz: | |
| print('xx <= zz') | |
| if xx == zz: | |
| print('xx == zz') | |
| if xx != yy: | |
| print('xx != yy') | |
| if xx == yy: | |
| print('xx == yy') | |
| else: | |
| print('xx != yy') | |
| # functions | |
| def exampleFn(): | |
| print('basic function') | |
| zzz = 3 + 9 | |
| print(zzz) | |
| exampleFn() | |
| def simpleFn(num1,num2): | |
| answer = num1 + num2 | |
| print('num1 is', num1) | |
| print('answer is', answer) | |
| simpleFn(1,2) | |
| simpleFn(num2 = 42, num1 = 31) | |
| # simpleFn(42, 31, 20) # will throw error | |
| # simpleFn(30) # will throw error | |
| # function default parameters | |
| def defaultParams(num1=2,num2=3): | |
| return num1 + num2 | |
| print(defaultParams()) | |
| # global and local variables | |
| g = 4 | |
| def globalExample(): | |
| global g | |
| print(g) | |
| g += 6 | |
| print(g) | |
| globalExample() | |
| def localExample(): | |
| globg = g | |
| print(globg) | |
| globg += 10 | |
| print(globg) | |
| return globg | |
| g = localExample() | |
| print(g) | |
| # writing to file | |
| text = 'Sample text to save\nThis is a new line' | |
| saveFile = open('exampleFile.txt', 'w') | |
| saveFile.write(text) | |
| saveFile.close() | |
| # appending to file | |
| moreText = '\nMore text to append to the same file' | |
| appendFile = open('exampleFile.txt', 'a') | |
| appendFile.write(moreText) | |
| appendFile.close() | |
| # reading a file | |
| readFile = open('exampleFile.txt', 'r') | |
| print(readFile.read()) | |
| readFile.close() | |
| readFile = open('exampleFile.txt', 'r') | |
| print(readFile.readlines()) | |
| readFile.close() | |
| # classes | |
| class calculator: | |
| def addition(x,y): | |
| print(x + y) | |
| def subtraction(x,y): | |
| print(x - y) | |
| def multiplication(x,y): | |
| print(x * y) | |
| def division(x,y): | |
| print(x / y) | |
| calculator.addition(2,2) | |
| calculator.subtraction(2,2) | |
| calculator.multiplication(4,4) | |
| calculator.division(6,2) | |
| # shebang line - defines the path to python on linux | |
| #!/usr/bin/python | |
| # inside modules | |
| def epic(): | |
| print('wow this is great!') | |
| # if this module is the main file of the program, run print function | |
| if __name__ == '__main__': | |
| print('such great module') | |
| # user input | |
| inputVal = input('What is your name?') | |
| print(inputVal) | |
| # statistics | |
| import statistics | |
| example_list = [1,4,3,7,6,8,9,12,65,43,80,64,2,53,4] | |
| mean = statistics.mean(example_list) | |
| print(mean) | |
| median = statistics.median(example_list) | |
| print(median) | |
| mode = statistics.mode(example_list) | |
| print(mode) | |
| stdev = statistics.stdev(example_list) | |
| print(stdev) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment