AtBS 1-6 Flashcards

1
Q

An interactive shell is

A

a program that lets you type instructions into the computer and run one at a time

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

A file editor is..

A

Similar to a text editor, can write whole program in it to be run

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

How to print something to the shell?

A

print(‘Message’)

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

How to find length of a string?

A

print(len(myString))

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

How to write an if statement in Python?

A

if variable ==’input’:

do something

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

How to add else statement to if statement?

A

if variable ==’input’: do something

else: do something else

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

What does elif mean?

A

Else If

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

How to comment in Python

A

Use the pound sign #

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

Key command to interrupt Python?

A

Ctrl+C

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

What does a continue statement do?

A

Jumps back to the start of the loop to reevaluate the loops condition

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

What does a break statement do?

A

It gets out of loop if execution leads to it.

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

How to input a number as an integer?

A

Variable=int(input())

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

Formula for Gauss Loop

A

total=0

for num in range(101):

total=total+num

print(total)

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

What is the standard library?

A

It is the set of modules that Python comes with; Each module has a related group of functions that can be used in programs ex. random and math module

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

An argument is..

A

The value that a function evaluates ex print(‘argument’) len(argument)

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

What are the three boolean operators?

A

and

or

not

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

What are the six comparison operators?

A

>

<

<=

>=

==

!=

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

How to handle exceptions?

A

Use Try and Except

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

What is a return value?

A

It is the value that a function evaluates to

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

If you had a function name bacon() in a module name spam, how would you call it after importing spam?

A

Can be called with spam.bacon

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

Syntax to create a list

A

spam=[‘cat’,’bat’,’rat’,’elephant’]

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

Syntax to add to a list

A

spam=spam+[‘pteradactyl’]

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

Delete rat from following list [‘cat’, ‘mouse’, ‘rat’, ‘elephant’, ‘pteradactyl’]

A

del spam[2]

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

What do the in and not in operators do?

A

They can check whether a value is in a list or not.

They evaluate to True or False

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

What is an augmented assignment operator?

A

Instead of spam=spam+1

do spam+=1

This is shorter

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

How to find the position of a value in a list?

A

Use index method to find it spam.index(value)

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

How to add a value to a list?

A

use append method to add to a list spam.append(‘value’)

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

How to insert a value to a list?

A

use insert method to add to a list spam.insert(position num,value) This does not replace the value

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

How to replace a value in a list?

A

spam[num in list] = new value

30
Q

How to remove a value in a list?

A

Use remove method spam.remove(value)

31
Q

How to sort values in a list?

A

Use sort method spam.sort()

32
Q

How to reverse sort in a list?

A

spam.sort(reverse=True)

33
Q

What is a tuple?

A

Like list but they are immutable and typed in parentheses ()

34
Q

When you assign a list to a variable, what are you actually assigning?

A

You are actually assigning a reference and not the values themselves

35
Q

How to make separate copies of lists and not just references?

A

import copy module use copy.copy() function for lists

for list within list use deepcopy() function

36
Q

How to create a dictionary?

Dictionary Format

A

Use curly brackets

myCat={‘size’:’fat’}

Dictionary Name = {‘Key’:’Value’}

37
Q

How are dictionaries different than lists?

A

Dictionaries are unordered

Lists are ordered

38
Q

What are the advantages of dictionaries?

A

Dictionaries give the ability to have arbitrary keys.

ie name, age etc

39
Q

How to print all values in a dictionary?

A

for v in spam.values():

print(v)

40
Q

How to print all keys in a dictionary?

A

for v in spam.keys():

print(v)

41
Q

What is the combination of both a dictionary key and value called?

‘key’:’value’

A

It is called an item

42
Q

What does the get() Method do?

Syntax

A

It uses a key of a value to retrieve and a fallback option if that key is not found in a dictionary

ex spam.get(‘key’,’fallback’)returns either the value of the key inputted or ‘fallback’

43
Q

What does the setdefault() method do?

Syntax

A

It places a key and a value in a dictionary if not already there

spam .setdefault (‘key’, ‘value’)

44
Q

What does pprint function do to dictionaries?

Syntax

A

Prints the dictionary vertically

pprint.pprint(DictName)

45
Q

How to change a value in a dictionary?

Syntax

A

Dict_Name[‘key’] = ‘value’

46
Q

How to get pprint to only go to one line for each item to print in a dictionary?

A

pprint(DictName, width=1)

47
Q

What can having double quotes do for a string?

Example

A

They allow you to have a single quote character in the string.

Example

“This is Alice’s cat.”

48
Q

What is an escape character and what does it let you do?

A

It is character that lets you use other characters that you normally cannot use

In Python it is the backslash \

49
Q

Escape character to use

Single quote?

Double quote?

A

'

"

50
Q

Escape character to

Tab

Linebreak

Backslash

A

\t

\n

\

51
Q

What is a raw string?

Syntax

When is it useful?

A

A raw string completly ignores all escape characters and will print the entirety between quotes as a string.

print(r’That is Carol's cat.’)

It is useful for regular expressions.

52
Q

What is a multiline string?

A

A multiline string starts off with 3 single or double quotes

It lets you type multiple lines as a string

It ends with 3 single or double quotes

53
Q

How to write multiline comments in python?

A

””” use the multiline string of

3 quotes before

comments

and 3 quotes after “””

54
Q

How does slicing a string work?

spam=’Hello World’

spam[0] returns

spam[0:2] returns

A

If you specify one index position Python returns that position in a string

If you specify a range Python includes beginning index but not the ending index

spam[0] returns ‘H’

spam[0:2] returns ‘He’

55
Q

spam=’Hello World!’

slice the string to say ‘Hello’

slice the string to say ‘World!’

A

spam[:5] ‘Hello’

spam[6:] ‘World!’

56
Q

spam=’Hello World!’

What would the methods

spam = spam.upper()

spam = spam.lower()

A

upper() =’HELLO WORLD!’

lower()= ‘hello world!’

57
Q

What does

isupper() and islower()

A

Return True if all letters are upper or lower

False if no letters or a mix of upper and lower

58
Q

What do the following methods do?

isalpha()

isalnum()

isdecimal()

A

isalpha() True if all letters

isalnum() True if only numbers and letters

isdecimal() True if only numbers

59
Q

What do the following methods do?

isspace()

istitle()

A

isspace() True if string only spaces, tabs and newlines

istitle() True if string only has uppercase letter folowed by undercase

60
Q

What do the startswith() and endswith() methods return?

A

The return either true or false if a string begins or ends with the value

ex

spam=(‘The world’)

spam.startswith(‘The’)

returns True

61
Q

What does the join property do?

Syntax?

A

Join a series of strings into one string

’ ‘.join([‘My’,’name’, ‘is’, ‘Simon’])
‘My name is Simon’

or

’ @’.join([‘My’,’name’, ‘is’, ‘Simon’])

‘My @name @is @Simon’

62
Q

What does the split method do?

A

It splits one string into multiple strings, usually based on whitespace but you can set it for other items as well

‘My name is Simon’.split(‘m’)
[‘My na’, ‘e is Si’, ‘on’]

63
Q

What kind of brakets for:

list

dictionary

tuple

A

list []

dictonary{}

tuple()

64
Q

What do the rjust() and ljust() methods do?

Syntax?

A

They return a padded version of the string that they are on.

‘Hello’.rjust(10)

returns

’ Hello’

65
Q

What does

‘Hello’.rjust(20,’*’) return?

A

’***************Hello’

66
Q

What does

‘Hello’.center(20,’$’) return

A

’$$$$$$$Hello$$$$$$$$’

67
Q

What do the strip(), rstrip() and lstrip methods do?

A

Remove whitespace from string.

L and R remove it from left and right sides

OR it can specify certain charcters to be stripped from the ends

68
Q

spam=’Blue Bell Ice Cream’

What will

spam.strip(‘amBl’) return?

A

‘ue Bell Ice Cre’

69
Q

How to install third party modules?

A

Open command line

pip install ModuleName

70
Q

How to copy text from clipboard so python can use it?

A

New Variable=pyperclip.paste()