Python General Revision Flashcards
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
datetime
datetime.now
datetime is a type of data (date and time).
datetime.now prints current date and time
from command
import command
from = data location
import = get data
hot commands and placeholders
in printing datetime context
placeholder = %s hot command = % from datetime import datetime now = datetime.now() print '%s/%s/%s %s:%s:%s' % (now.day, now.month, now.year, now.hour, now.minute, now.second) 08/08/2014 13:56:23
Control Flow
Enables code to make decisions
Control Flow: Comparators
equal to == not equal to != less than < greater than > less than or equal to =
Control Flow: Expressions
expressions are values that can have operations like maths, boolean, if, or, and, not, nor, nand, etc.
18 >= 16
extreme expressions and comparators
(10 + 17) == 3 ** 16
Control Flow: Boolean operators
not and or
not is calulated 1st
and is 2nd
or is last
true or false (3 < 4) and (5 >= 5) this() and not that()
Control Flow: Conditional statements
if elif else OR def chess_game_score (answer): if this_might_be_true(): if (answer) > 5: print "this is true" return "true" elif that_might_be_true(): elif (answer) (3 < 4) and (5 >= 5): print "this is really true" return "false" else: else: print "none of the above" return "neither"
.isalpha
checks strings for non letters
what does a comma do in a print statement of variables? eg variable_1 = "hello" variable 2 = "chris" print variable_1, variable_2
creates a space between variable strings
eg it will print this
hello chris