PyGenCon Flashcards
Variable
A CONTAINER that stores data/values as a specific name
ie spam = 5
data type
numbers, text, booleens etc
whitespace
seperates statements
whitespace means right space
comments
to make code easier to follow
# for single line comment
“"”for multi line comments”””
boolean
true or false statements
variables can store booleans
ie: a = true
b = false
Python order of operation
maths PEMDAS
python uses PEMDAS system:
Parentheses ( …) = everything in brackets first
Exponents ** = powers (5 ^ 3 is 5x5x5). written as 5 ** 5 in python
Multiplication
Division
Addition, Subtraction
modulo (Mod)
modulo returns the remainder from a division.
ie. 3%2 will return 1. 2 goes into 3 once with 1 remaining (remember the remaider is the bit modulo is interested in)
String
A string is a data type which is written in “quotation marks” ie “ryan”
It can contain numbers, letters and symbols.
Each character in a string is assigned a number (the index) starting with 0.
String method
lets you perform specific tasks for strings ie:
len() = length of string
str() = turns non-string into string ie. str(2) into “2”
. lower() = makes string all lowercase ie. “Ryan” .lower() into “ryan”
. upper() = makes string all uppercase
Dot Notation
this is strings with dot notation in!
ie “Ryan” .upper() or “Ryan”.lower()
but it could be one of many dot .() commands
String Concatenation
This is combining strings with math operators
ie print “life + of + brian” becomes life of brian
Explicit String Conversion
converts non string to string ie:
print “I have” + str(2) + “coconuts!”
will print
I have 2 coconuts!
String Formatting
printing a variable with a string %s goes in the string % goes after string string1 = "Camelot" string2 = "place" print "let's not go to %s tis a silly %s" % (string1, string2)
3 ways to create string
‘Ryan’
“Ryan”
str(2)
- print a string
2. advanced printing
- print “Hello” Hello
- g = “golf”
h = “hotel”
print “%s, %s” % (g, h)
golf hotel