|
name
|
value
|
|
string
|
"anything1234!@£$%"
|
|
integer
|
1234
|
|
float
|
1.2345
|
|
bool
|
True/False
|
|
1. Easy to remember
|
|
2. Short
|
|
3. No spaces
|
|
4. Cannot start with a number
|
|
5. Can only contain letters, numbers and underscores
|
|
Name
|
Usage
|
Code
|
|
List
|
Multiple items in one variable
|
["apple", "banana", "strawberry"]
|
|
Dictionary
|
Multiple key-value pairs in one variable
|
{"hello": "world"}
|
|
cast
|
what it does
|
|
str()
|
converts to a string
|
|
int()
|
converts to an integer
|
|
float()
|
converts to a float
|
name = "abcdef123"
age = 18
height = 3.14
|
print("text")
|
outputs text to screen
|
|
input("text")
|
prints text and allows input from a user
|
|
round(3.14)
|
rounds a float down to an integer
|
|
symbol
|
meaning
|
|
==
|
equal to
|
|
>
|
larger than
|
|
<
|
less than
|
|
>=
|
larger than or equal
|
|
<=
|
less than or equal
|
|
!=
|
not equal
|
|
and
|
both values must be true
|
|
or
|
either value may be true
|
|
xor
|
either value may be true, but not both
|
|
+
|
add
|
|
-
|
subtract
|
|
*
|
multiply
|
|
/
|
divide
|
|
**
|
power
|
|
%
|
modulus
|
value = 3
if value < 4:
print("less than 4")
elif value > 2:
print("larger than 2")
else:
print("some other value")
# 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)
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.