Programming Cheat Sheet

A cheat sheet in English by godley
5,214 views 2,273 downloads 4 Aug 2015 History
3.7 avg · 0 ratings

Data types

name
value
string
"­any­thi­ng1­234­!@£­$%"
integer
1234
float
1.2345
bool
True/False

Variable naming rules

1. Easy to remember
2. Short
3. No spaces
4. Cannot start with a number
5. Can only contain letters, numbers and unders­cores

Collec­tions and generics

Name
Usage
Code
List
Multiple items in one variable
["ap­ple­", "­ban­ana­", "­str­awb­err­y"]
Dictionary
Multiple key-value pairs in one variable
{"he­llo­": "­wor­ld"}

Casting

cast
what it does
str()
converts to a string
int()
converts to an integer
float()
converts to a float
Casting is used to convert between values. It needs to occur before doing any mathem­atical calcul­ations with an input() from a user because user input is always strings.

Variable assignment

name = "abcdef123"
age = 18
height = 3.14

Useful python functions

print(­"­tex­t")
outputs text to screen
input(­"­tex­t")
prints text and allows input from a user
round(­3.14)
rounds a float down to an integer

Compar­ators

symbol
meaning
==
equal to
>
larger than
<
less than
>=
larger than or equal
<=
less than or equal
!=
not equal

Boolean operators

and
both values must be true
or
either value may be true
xor
either value may be true, but not both

Arithmetic operators

+
add
-
subtract
*
multiply
/
divide
**
power
%
modulus

Condit­ionals

value = 3
if value < 4:
        print("less than 4")
elif value > 2:
        print("larger than 2")
else:
        print("some other value")
else and elif must follow an if. Else cannot come before elif. Whitespace before the print statements indicates they are only to print if the condition is met

Loops

# infinite loop
while True:
        print("cheese!")

# counter loop - 0-9
for i in range(10):
        print(i)

# iterating a list
collection = ['apple', 'orange', 'banana']
for value in collection:
        print(value)

Badges

Good Cheat Sheet Nice Cheat Sheet

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.