Python General Revision Flashcards

1
Q

data type

A

numbers, text, booleans etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

whitespace

A

seperates statements

whitespace means right space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

comments

A

to make code easier to follow
# for single line comment
“"”for multi line comments”””

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

boolean

A

true or false statements

variables can store booleans
ie: a = true
b = false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Python order of operation

maths PEMDAS

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

modulo (Mod)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

String method

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dot Notation

A

this is strings with dot notation in!
ie “Ryan” .upper() or “Ryan”.lower()
but it could be one of many dot .() commands

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

String Concatenation

A

This is combining strings with math operators

ie print “life + of + brian” becomes life of brian

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explicit String Conversion

A

converts non string to string ie:
print “I have” + str(2) + “coconuts!”
will print
I have 2 coconuts!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

String Formatting

A
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

3 ways to create string

A

‘Ryan’
“Ryan”
str(2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. print a string

2. advanced printing

A
  1. print “Hello” Hello
  2. g = “golf”
    h = “hotel”
    print “%s, %s” % (g, h)golf hotel
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

datetime

datetime.now

A

datetime is a type of data (date and time).

datetime.now prints current date and time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

from command

import command

A

from = data location

import = get data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

hot commands and placeholders

in printing datetime context

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Control Flow

A

Enables code to make decisions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Control Flow: Comparators

A
equal to ==       
not equal to !=      
less than <      
greater than >
less than or equal to =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Control Flow: Expressions

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Control Flow: Boolean operators

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Control Flow: Conditional statements

A
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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

.isalpha

A

checks strings for non letters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
what does a comma do in a print statement of variables?
eg
variable_1 = "hello"
variable 2 = "chris"
print variable_1, variable_2
A

creates a space between variable strings
eg it will print this
hello chris

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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?

A

chris = “ %r %r %r”

print chris % (“25”, “years”, “old”)

25 years old

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

what does \n do in front of data types in a string?

A

\n creates a new line between each data type in a string
eg:
variable_1 = “\nchris\ngareth\njason
print variable_1

chris
gareth
jason

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

what does using “”” instead of “ at the beginning and end of a string allow you to do?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

what does the raw_input command do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

what is pydoc?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

what is a script?

A

another name for a .py file type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

what is an argument?

A

another name for a file name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

what does an argv function do?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

what are modules?

A

Features you import to make your python program do more.

also called libraries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

open

A

opens file

35
Q

how to give files commands

A

put a . after file or variable name

txt = open(filename)
print txt.read

txt is a variable which opens a file. print txt.read tells program to read that files text and print it.

36
Q

function and method are other names used for?

A

a command

37
Q

what is a command

A

instruction to program to do something with a file
eg open, print, read

read command reads data in a given file and when combined with the print command will print it ( print txt.read)

38
Q

Reading and writing files

What do close, read, readline, truncate and write commands do?

A
close = save and close ("c")
read = read data from file ("r")
readline = read just one line of data from a file 
truncate = empties file. delete all dat in file
write = write data to file

can be added as an extra parametre
eg target = open(filename, “w”)
this mean open file in write mode (as opposed to read mode)

39
Q

do you need a truncate command when using a write command?

A

no, write command will overwrite data in file so no need for truncate (erase file contents) command.

40
Q

what are packages?

A

directories (folders)

41
Q

what are modules?

A
files
You know that a module is a Python file with some functions or variables in it.
2. You then import that file.
3. And then you can access the functions or variables in that module with the '.' (dot) operator.
 In the case of the module the key is an identifier, and the syntax is .key. In the case of the dictionary, the key is a string and the syntax is [key].Other than that, they are nearly the same thing.

mystuff[‘apple’] # get apple from dict mystuff.apple() # get apple from the module mystuff.tangerine # same thing, it’s just a variable

42
Q

what are modules?

A
files, arguments
A way to think about a module is that it is a specialized dictionary that can store Python code so you can get to it with the '.' operator. 
You know that a module is a Python file with some functions or variables in it.
2. You then import that file.
3. And then you can access the functions or variables in that module with the '.' (dot) operator.
 In the case of the module the key is an identifier, and the syntax is .key. In the case of the dictionary, the key is a string and the syntax is [key].Other than that, they are nearly the same thing.

mystuff[‘apple’] # get apple from dict mystuff.apple() # get apple from the module mystuff.tangerine # same thing, it’s just a variable

43
Q

why is closing files at end of code important?

A

close command not only closes file but saves it too.
having lots of open file is not good, there is also a limit to amount of open files at same time.
good practice.

44
Q

files are also known as

A

modules, arguments

45
Q

what does close command do?

A

closes and saves files

output_file.close

46
Q

what does exists command do?

A

looks for a specified file and return True statement if it already exists, and False if it doesnt.

47
Q

what is os.path?

A

a location on computer containing modules (files) of useful commands to use in programming.

48
Q

what 3 things do functions do?

A
  1. they are commands/methods that call/run sub-routines!!!
  2. they take arguments the way scripts take argv
  3. they use both the above to enable you to make ‘mini scripts’ or ‘tiny commands’.
  4. names a peice ???of the way variable name strings and numbers
49
Q

what are definitions? def

A
  1. a definition is a keyword that assigns a subroutine with a function or command to a variable.
  2. it must end its first command line with :
  3. the def variable can have other variables as parameters that it can call/run. def variable_1(variable_2, variable_3):
    the subroutine of a def must be indented so the program knows that any code that is indented is part of that subroutine.
    def variable_1(variable_2, variable_3):
    print variable_2, variable_3.read()
50
Q

truth tables for NOT, OR, AND, NOR, NAND

A

NOT: not false = true, not true = false 0=1 1=0
OR: false or false = false 0 or 0 = 0
NOR: false and false = true 0 or 0 = 1
AND:true and true = true 1 and 1 = 1
NAND: true and true = false 1 and 1 = 0

51
Q

==

!=

A

equal to 1 == 1 is true 1 == 0 is false

not equal to 1 != 1 is false 1 != 0 is true

52
Q

a += 1
a -= 1
a *= 1
a /= 1

A
a = a + 1
a = a - 1
a = a * 1
a = a / 1
53
Q

if statements and how they work.

A

if, elif and else
logic decision that create branches that can:
make decisions themselves
make decision themselves and direct to specified areas of program or to sub routines or other branches (if statements)

54
Q

nests

A

if statements within if statements

or branches within branches

55
Q

what are lists?

A

containers that store data types in an organised manner
data types in lists are assign values starting at 0 (cardinal)
you can then use these values/numbers to index into a list to find specific data.
you can only use number to index
If you need to maintain order. Remember, this is listed order, not sorted order. Lists do not sort for you.
If you need to access the contents randomly by a number. Remember, this is using cardinal numbers starting at 0.
If you need to go through the contents linearly (first to last). Remember, that’s what for-loops are for.

56
Q

.append

A

command that adds something to the end

57
Q

what are lists?

A

containers that store data types in an organised manner
data types in lists are assign numeric values starting at 0 (cardinal)
you can only use numbers to index into a list to find and retrieve specific data.
The order you put things in list is the number order for retrieval. Remember, this is listed order, not sorted order. Lists do not sort for you.

you can access contents randomly or specifically anywhere in the list by using numbers
use a for-loop to go through the contents linearly (first to last).

58
Q

while-loop function

A

loops through list till an assigned criteria is met

while i < 6:
print “not yet”

59
Q

what are def, for and while?

A

def, for and while are keywords that call sub routines

60
Q

floating point method

A

approximation of a real number that can support a wider range of values
Basically this is using averages to represent a number
ie:
range for a number (difference between smallest and largest number)
median (the middle number in a range [number need to be put in order first])
mode (the number repeated most often in a range)
mean (the average number in a range)

61
Q

fixed point method

A

represents a real data type for a number with fixed digits before and after a decimal point
it is an integer (int)
it always have a parameter/factor attached to it most commonly an exponent or power of 10 or 2
20 ** 2
20 ** 10

62
Q

base 2

A

a binary numeric value

think computer maths logic

63
Q

base 10

A

a decimal numeric value

think human maths logic

64
Q

hexadecimal or base 16

A

counting system commonly used in programming using a 16 digit scale:
hex 0123456789 a b c d e f
dec 0123456789101112 13 14 15
written as ‘0x’ in code

65
Q

ASCII

A

english language characters

66
Q

Unicode

A

universal code
multi language characters

UTF-8
1 byte = standard ASCII
2 bytes = Arabic, Hebrew and most European scripts
3 bytes = BMP
4 bytes = All Unicode characters
67
Q

concatenation

A

joining strings together

“snow” “ball” becomes “snowball”

68
Q

OOP

A

Object-oriented programming (OOP) is a programming paradigm that represents the concept of “objects” that have data fields (attributes that describe the object) and associated procedures known as methods.

Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.[1][2] C++, Objective-C, Smalltalk, Delphi, Java, Javascript, C#, Perl, Python, Ruby and PHP are examples of object-oriented programming languages.

69
Q

instances

A

??

70
Q

Variable

A

A CONTAINER that stores data/values as a specific name

ie spam = 5

71
Q

class

A

Classes Are Like Mini-Modules

A class is a way to take a grouping of functions and data and place them inside a container so you can access them with the ‘.’ (dot) operator.

A container for objects, functions , data.

class song(object):

def \_\_init\_\_(self, lyrics):

when assigning an object as a parameter of a class, the first parameter variable of __init__ becomes object. the word self is used as good practice, however it is just a variable and can be called anything you want.

72
Q

objects

A

Objects Are Like Mini-Imports

If a class is like a “mini-module,” then there has to be a similar concept to import but for classes. That concept is called “instantiate,” which is just a fancy, obnoxious, overly smart way to say “create.” When you instantiate a class, what you get is called an object.

The way you do this is you call the class like it’s a function, like this:
thing = MyStuff() 
thing.apple()
print thing.tangerine
73
Q

Dictionaries

A
In python called dicts (hashes in other languages).
It maps one data type/item (called keys) to another data type/item (called values). (like in a dictionary maps a word to its definition)
In the case of the dictionary, the key is a string and the syntax is [key]. In the case of the module the key is an identifier, and the syntax is .key. Other than that, they are nearly the same thing.

Are a type of data structure like lists.
Unlike lists dicts allow you to index/locate data with anything not just numbers.
It associates one thing with another.
{ } brackets.
Dictionaries allow you to add things to them in other parts of your program.
They also allow you to delete things from them.
They can have numbers as parameters in [ ] that are assigned to them as printable.

mystuff[‘apple’] # get apple from dict mystuff.apple() # get apple from the module mystuff.tangerine # same thing, it’s just a variable

74
Q

cardinal numbers start at 1 or 0?

A

0

75
Q

index

A

the assignment of numeric values to data.
usually used in lists etc where each bit of data in the its has a number assigned to it. this is assigned in the order data is in the list using cardinal numbering system.

76
Q

delete items from a list

A

del stuff[1]

77
Q

delete items from a dict

A

del stuff[‘city’]

78
Q

getting things from classes

A

thing = MyStuff()
thing.apples()
print thing.tangerine

79
Q

getting things from modules

A

mystuff.apples()

print mystuff.tangerine

80
Q

getting things from dicts

A

mystuff[‘apples’]

81
Q

(variable1, variable2 )

A

variables as parameters

82
Q

([‘apple’, ‘cheese’, biscuit’ ])

A

lists as parameters

83
Q

(“hello silly”, “cheese please”)

A

strings as parameters

84
Q

(10, 2)

A

numbers as parameters