-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheatsheet.py
More file actions
108 lines (88 loc) · 2.7 KB
/
cheatsheet.py
File metadata and controls
108 lines (88 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Python cheet sheet for Samuel and Kishan's python workshop
# Don't try to run this file; you'll just get a lot of undefined
# variable errors
# define variable a to be 5
a = 5
# define variable b to be the string: Hello World!
b = "Hello World!"
# print(...) prints whatever is inside the parenthesis to the console
print("Wheee!")
# if you want to be able to change the data that your program uses while
# it's running, here's how you can get information from the user;
string = input("Please type a number: ")
# asks the user to type a number and press enter. what the user types gets
# stored in the data variable.
# note that the response will always be a string, not a number.
# (even if that's what you prompted the user for.)
# to convert a string into a number:
x = float(string)
# numbers can be added like this:
a + b
# they can be subtracted:
a - b
# multiplied:
a * b
# divided:
a / b
# raise a to the power of b
a ** b
# to find the square root of a number:
math.sqrt(x)
# but remember to to this sometime beforehand:
import math
# alternatively, you can raise x to the one-half power
x ** (1/2)
# to define a function called foo:
def foo():
print("bar!")
# after you do this, anytime you say:
foo()
# bar! will be printed
# functions can also take argments (a.k.a. parameters):
def quux(a, b):
c = a + b
print(c)
# the above function takes 2 arguments, adds them, and prints the result.
# functions can also return values; translating from math,
# a Python function's return value is very similar to the evaluated value
# of a function in math.
# for example: y = 3 * x is expressed in Python as:
def multiply_by_three(x):
return 3 * x
# you can test if a is less than b like so:
a < b
# the result of this expression is True or False depending on whether
# a is actually less than b
a > b
# checks if a is greater than b
a == b
# checks if a and b are equal
# now suppose that we only want to do something if a certain condition
# holds true (like x is greater than 4)
if x > 4:
print("Guess what? x is greater then 4!")
# but what if we want to do something else if x isn't greater than 4?
if x > 4:
print("Guess what? x is greater then 4!")
else:
print("Oh crap! Whatever happened to x?")
# Well that's all well and good, but what about those piecewise functions
# from math?
if x < 2:
y = 3 + x
elif x > 2:
y = x * 17
else:
print("x is 2! value undefined!")
# One thing computers are really great at is doing lots of repetitious tasks.
# like processing hundreds of personell records...
# How do we do stuff like this?
# for example, suppose we want to find all of the fibonacci numbers
# less than 1000:
a = 1
b = 1
while b < 1000:
print(b)
c = a + b
b = a
a = c