AtBS 1-6 Flashcards
An interactive shell is
a program that lets you type instructions into the computer and run one at a time
A file editor is..
Similar to a text editor, can write whole program in it to be run
How to print something to the shell?
print(‘Message’)
How to find length of a string?
print(len(myString))
How to write an if statement in Python?
if variable ==’input’:
do something
How to add else statement to if statement?
if variable ==’input’: do something
else: do something else
What does elif mean?
Else If
How to comment in Python
Use the pound sign #
Key command to interrupt Python?
Ctrl+C
What does a continue statement do?
Jumps back to the start of the loop to reevaluate the loops condition
What does a break statement do?
It gets out of loop if execution leads to it.
How to input a number as an integer?
Variable=int(input())
Formula for Gauss Loop
total=0
for num in range(101):
total=total+num
print(total)
What is the standard library?
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
An argument is..
The value that a function evaluates ex print(‘argument’) len(argument)
What are the three boolean operators?
and
or
not
What are the six comparison operators?
>
<
<=
>=
==
!=
How to handle exceptions?
Use Try and Except
What is a return value?
It is the value that a function evaluates to
If you had a function name bacon() in a module name spam, how would you call it after importing spam?
Can be called with spam.bacon
Syntax to create a list
spam=[‘cat’,’bat’,’rat’,’elephant’]
Syntax to add to a list
spam=spam+[‘pteradactyl’]
Delete rat from following list [‘cat’, ‘mouse’, ‘rat’, ‘elephant’, ‘pteradactyl’]
del spam[2]
What do the in and not in operators do?
They can check whether a value is in a list or not.
They evaluate to True or False
What is an augmented assignment operator?
Instead of spam=spam+1
do spam+=1
This is shorter
How to find the position of a value in a list?
Use index method to find it spam.index(value)
How to add a value to a list?
use append method to add to a list spam.append(‘value’)
How to insert a value to a list?
use insert method to add to a list spam.insert(position num,value) This does not replace the value
How to replace a value in a list?
spam[num in list] = new value
How to remove a value in a list?
Use remove method spam.remove(value)
How to sort values in a list?
Use sort method spam.sort()
How to reverse sort in a list?
spam.sort(reverse=True)
What is a tuple?
Like list but they are immutable and typed in parentheses ()
When you assign a list to a variable, what are you actually assigning?
You are actually assigning a reference and not the values themselves
How to make separate copies of lists and not just references?
import copy module use copy.copy() function for lists
for list within list use deepcopy() function
How to create a dictionary?
Dictionary Format
Use curly brackets
myCat={‘size’:’fat’}
Dictionary Name = {‘Key’:’Value’}
How are dictionaries different than lists?
Dictionaries are unordered
Lists are ordered
What are the advantages of dictionaries?
Dictionaries give the ability to have arbitrary keys.
ie name, age etc
How to print all values in a dictionary?
for v in spam.values():
print(v)
How to print all keys in a dictionary?
for v in spam.keys():
print(v)
What is the combination of both a dictionary key and value called?
‘key’:’value’
It is called an item
What does the get() Method do?
Syntax
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’
What does the setdefault() method do?
Syntax
It places a key and a value in a dictionary if not already there
spam .setdefault (‘key’, ‘value’)
What does pprint function do to dictionaries?
Syntax
Prints the dictionary vertically
pprint.pprint(DictName)
How to change a value in a dictionary?
Syntax
Dict_Name[‘key’] = ‘value’
How to get pprint to only go to one line for each item to print in a dictionary?
pprint(DictName, width=1)
What can having double quotes do for a string?
Example
They allow you to have a single quote character in the string.
Example
“This is Alice’s cat.”
What is an escape character and what does it let you do?
It is character that lets you use other characters that you normally cannot use
In Python it is the backslash \
Escape character to use
Single quote?
Double quote?
'
"
Escape character to
Tab
Linebreak
Backslash
\t
\n
\
What is a raw string?
Syntax
When is it useful?
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.
What is a multiline string?
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
How to write multiline comments in python?
””” use the multiline string of
3 quotes before
comments
and 3 quotes after “””
How does slicing a string work?
spam=’Hello World’
spam[0] returns
spam[0:2] returns
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’
spam=’Hello World!’
slice the string to say ‘Hello’
slice the string to say ‘World!’
spam[:5] ‘Hello’
spam[6:] ‘World!’
spam=’Hello World!’
What would the methods
spam = spam.upper()
spam = spam.lower()
upper() =’HELLO WORLD!’
lower()= ‘hello world!’
What does
isupper() and islower()
Return True if all letters are upper or lower
False if no letters or a mix of upper and lower
What do the following methods do?
isalpha()
isalnum()
isdecimal()
isalpha() True if all letters
isalnum() True if only numbers and letters
isdecimal() True if only numbers
What do the following methods do?
isspace()
istitle()
isspace() True if string only spaces, tabs and newlines
istitle() True if string only has uppercase letter folowed by undercase
What do the startswith() and endswith() methods return?
The return either true or false if a string begins or ends with the value
ex
spam=(‘The world’)
spam.startswith(‘The’)
returns True
What does the join property do?
Syntax?
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’
What does the split method do?
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’]
What kind of brakets for:
list
dictionary
tuple
list []
dictonary{}
tuple()
What do the rjust() and ljust() methods do?
Syntax?
They return a padded version of the string that they are on.
‘Hello’.rjust(10)
returns
’ Hello’
What does
‘Hello’.rjust(20,’*’) return?
’***************Hello’
What does
‘Hello’.center(20,’$’) return
’$$$$$$$Hello$$$$$$$$’
What do the strip(), rstrip() and lstrip methods do?
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
spam=’Blue Bell Ice Cream’
What will
spam.strip(‘amBl’) return?
‘ue Bell Ice Cre’
How to install third party modules?
Open command line
pip install ModuleName
How to copy text from clipboard so python can use it?
New Variable=pyperclip.paste()