Section 1

Preview this deck

Extend a string over multiple lines

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (77)

Section 1

(50 cards)

Extend a string over multiple lines

Front

Use triple quotes: s = """ blah blah blah """

Back

access 7 in aList = [42, [1, 2, 7]]

Front

aList[1][ 2]

Back

To create an instance from a module

Front

1. import the module import math 2. create instance using module name: myObj = math.set()

Back

Indicator of list in the initialization phase

Front

[]

Back

shift left and right by n bits

Front

x << n, x >> n

Back

start index of list

Front

0

Back

bitwise and, or, not:

Front

x & y, x or y, ~x

Back

divmod(x, y)

Front

the pair (x // y, x % y)

Back

Length of a list

Front

len(aList)

Back

Initialize dict:

Front

- dict1 = {} dict1['list'] = 'a collection' -dict = {'list' : 'a collection'}

Back

append a list to another list

Front

aList.append(another) print aList [42, [1, 2, 3]] (The list itself got appended, not the members). Note the difference with join(+)

Back

another[:2]

Front

Sublist ending at index 2

Back

store format string in a ...

Front

string variable s = "The sum of %d, %d, %d is %d" print s % (v, x, y)

Back

Populate a list with multiples of same value

Front

zeroList = [0] * 5

Back

int(x), long(x), float(x), complex(x, y)

Front

converting a number to int, long, float, complex

Back

What indexes tuples

Front

square brackets: [] aTuple[2]

Back

List of keys in a dictionary

Front

dict1.keys() dict.values()

Back

Negative index in list

Front

Access members from the end of the list

Back

iterate through lines in the file

Front

for line in file_handler:

Back

bitwise xor

Front

x ^ y

Back

We initialize dictionary with ... and access using ...

Front

Initialize: {} Access: []

Back

Make a number complex

Front

Append a J at the end: a = 1J (the real part will be zero) a.real, a.imag

Back

create empty list

Front

aList = []

Back

Convert to int / float / str

Front

(int)a / (float)a / (str)a

Back

Unlike tuples strings are

Front

Appendable using +. This results in a new string

Back

dict contents as list of tuples (tuple is like a little list)

Front

dict.items()

Back

(Array = vector) is a ... list

Front

Fixed size

Back

3 in another

Front

Test for containment

Back

a = 'hello and goodbye'.split()

Front

split string on whitespace to return a list

Back

Can we change tuples after creation?

Front

No. they are immutable. Cannot change or append to them

Back

Can you change values of chars within a string?

Front

No

Back

print list

Front

print aList

Back

another[-1]

Front

get the last item

Back

and, or, not

Front

a and b a or b not a

Back

pow(x, y) x ** y

Front

x to the power y

Back

remove element i of list aList

Front

del aList[i]

Back

Change the content of an index

Front

another[2] = 8

Back

create non-empty list

Front

another = [1, 2, 3]

Back

Concatenation, repetition, and appending in dictionaries ...

Front

Does not work

Back

What defines tuples

Front

Parentheses containing comma seperated list of values: aTuple = (1, 3, 5)

Back

strings are like list of chars, but strings are

Front

immutable

Back

Operator to access list member by index

Front

[]

Back

x // y

Front

(floored) quotient of x and y

Back

Make a number long

Front

append L at the end: a = 1L

Back

stack, queue, bag, set

Front

Not built in

Back

abs(x)

Front

absolute value or magnitude of x

Back

another[2:]

Front

Sublist starting from index 2

Back

Append a number to a list

Front

aList.append(42)

Back

Open file for reading

Front

file_handler = open('file_name', 'r')

Back

Concatinate (join) two lists

Front

newList = aList + another (note the diff with append)

Back

Section 2

(27 cards)

determine the number of elements in sys.argv by:

Front

len(sys.argv)

Back

Deep copy of a container

Front

l2 = l1(:) * note that l2 = l1 will make a shallow copy, so that changing l1 will also change l2

Back

Two ways of importing

Front

import sys sys.exit() from sys import * exit()

Back

for loop

Front

for j in xrange(2, 30):

Back

What is the program name then?

Front

sys.argv[0]

Back

else if

Front

elif

Back

Standard boilerplate at end of file to call main() function

Front

if __name__ == '__main__': main()

Back

Create a module

Front

Add the file path to the PYTHONPATH environment variable and simply import the file name as the module name in your code

Back

return four things from the function

Front

return (a, b, c, d) : It makes a tuple you can call: x, y, z, w = function_name()

Back

Take string and non-string input from the user

Front

string_output = raw_input("input message") number_output = input("input message")

Back

split string s using separator c = "separator_string"

Front

import string words = string.split(s, c)

Back

range creates a list, not including the second number

Front

y = range(start, stop[, step]) y = range(20)

Back

some functions and data members of os module

Front

-rename -mkdir -getcwd -system (execute an os level command)

Back

list functions in a module

Front

dir(module_name) ex: dir(__builtins__)

Back

some functions and data members of string module

Front

- atoi/f/l (convert string to integer/floating/long) -find (find substring) -split (break into words) -upper/lower (convert case)

Back

Define a function

Front

def function_name(args)

Back

xrange

Front

is a sequence object that evaluates lazily.

Back

some functions and data members of sys (Allows interaction with python system)

Front

-exit -argv -path (python system path) -ps1 (command prompt icon)

Back

How to access to the local variables of the function we just called?

Front

global name_of_the_variable

Back

The args of the program

Front

args = sys.argv[1:]

Back

Python is pass by ...

Front

value

Back

argument list is held within the ... module and called ... for ...

Front

sys, argv, argument values - The list is is sys.argv

Back

To return from a function using its argument

Front

- Make a list argument - Append the results to it

Back

How to return a list from a function

Front

return (list_name) *Don't forget the ()

Back

What holds the last result in interactive mode? (not assigned a value)

Front

_

Back

some functions and data members of time module

Front

- time (current time: seconds from some date in os) - gettime(convert time in secs to UTC) - localtime (convert to local time) - ex: time.localtime(time.time()) -sleep (pause program for some seconds) -mktime (opposite of localtime)

Back

Sort a list in reverse order

Front

sorted(a, reverse=True)

Back