Python General Revision Flashcards
data type
numbers, text, booleans 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 ** 3 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
how do you write a variable with a string of placeholders then set the data type or value of those place holders in the print statement?
chris = “ %r %r %r”
print chris % (“25”, “years”, “old”)
25 years old
what does \n do in front of data types in a string?
\n creates a new line between each data type in a string
eg:
variable_1 = “\nchris\ngareth\njason
print variable_1
chris
gareth
jason
what does using “”” instead of “ at the beginning and end of a string allow you to do?
It allows you to print data on continues lines.
print
“"”do this, do that, do nothing.
do something, do everything
dont do anything at all!”””
do this, do that, do nothing.
do something, do everything
dont do anything at all!
what does the raw_input command do?
enables user to input text
enables program to get data and data types from a user
enable program to use this data in program
enable program to print a string of data to user based on that input
what is pydoc?
pydoc is pythons documentation module which allows programmers to acces help files, generate html pages with doc specifics and find appropriate modules for particular jobs
what is a script?
another name for a .py file type
what is an argument?
another name for a file name
what does an argv function do?
the in built function argument variable calls/runs variables assigned to it that relate to file names and types/formats
from sys import argv
script, input_file = argv
so if i now typed ‘python exp20c.py test.txt’ in command line the code above would run both files. specifically it would run the program on the python file exp20c.py (script) and run it on the file test.txt (input_file)
what are modules?
Features you import to make your python program do more.
also called libraries