Beginning Python Flashcards

Foundations of Python

1
Q

Visualize how to import a library

A

import library

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

Visualize a for loop that uses a list and describe what each part is doing in the for loop

A

for side in [1,2,3,4]:
x.forward(100)
x.right(100)

side is a variable and the for is assigning a value/item in the list to the side variable each time the loop runs.

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

Visualize a list

A

list = [a,b,c,d]

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

Visualize a nested loop

A

for x in [1,2,3,4]:
t.forwad(100)
t.right(90)
for side in [1,2,3,4]:
t.forward(10)
t.right(90)

The loop runs 16 times. 4x4 = 16

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

What is a compound statement

A

A compound statement contains other statements inside of it.

like a for loop

They change the the control flow of information

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

Visualize how to use range in a for loop

A

For x in range(4):
print(x)

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

What is a method

A

A method is a function that’s associated with an object. They are used to give objects behaviors.
All methods are functions, but not all functions are methods.

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

What is a parameter?

A

A parameter is a variable that is defined in a function definition.

def spiral(sides)

Sides is the variable

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

What is an argument?

A

An argument is an input that we pass to a function.
Spiral(100). 100 is the argument.

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

What is the scope of a variable that is passed inside a function?

A

It is a local scope, and can only be used inside the function.

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

What is the scope of a variable passed outside of a function

A

It is a global scope and can be used both inside functions and outside functions.

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

What is a conditional statement?

A

A conditional statement tells python to run only when a condition is met.

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

Visualize a conditional statement

A

x = t.Turtle()

if x == 1:
x.color(‘blue’)
else:
x.color(‘yellow’)

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

What is the modulo operator %?

A

The modulo operator divides one number by another and then gives the remainder of that division.

5 % 1 is 0
5 % 2 is 1
6 % 3 is 0

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

What happens if a % b and b is bigger?

A

The remainder will be a:
For example:
7 % 10 gives the result 7
7 % 100 gives the result 7
7 % 1000 gives the result 7

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

Visualize how to use % in a for loop

A

for n in range(12):
if n % 3 == 0:
draw_triangle()
else:
draw_square()

The loop will run 12 times. When n is 0,3,6, or 9, the result will be 0

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

Visualize how to use rand.choice

A

import random

A.
color = random.choice([‘red’,’blue’,’green’])

B.
colors = [‘red’,’blue’,’green’]
color = random.choice(colors)

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

Visualize how to use to use random.randint

A

import random

roll_die = random.randint(1,6)

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

Visualize all of the operators

A

a == b, a < b, a > b, a <= b, a >= b, a != b

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

How does an elif work in an if statement? Visualize how to use an elif

A

Nesting if else statements is the same as using elif

if mood == “happy”:
color = “pink”
elif mood == “calm”:
color = “lightBlue”
else:
color = “red”

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

What is Bash?

A

Bash, short for Bourne-Again SHell, is a shell program and command language supported by the Free Software Foundation and first developed for the GNU Project by Brian Fox

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

What do you type for BASH to recall your last command?

A

!!

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

Visualize how to create a variable in bash

A

x=100
echo $x
100

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

How do you list the contents of the current directory in BASH?

A

ls

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

How do you change directory in BASH? How do you go up one directory?

A

cd
cd ..

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

A. How do you make a directory in BASH?
B. How do you move folders?
C. Visualize how to make a directory and move .jpg and .gif into the folders?
D. How would you check the files moved?

A

A. mkdir
B. mv
C.
mkdir Photos
mv *.jpg Photos
mkdir Animations
mv *.gif Animations

D. ls

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

How do you rename files in BASH?

A

mv old_filename.txt new_filename.txt

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

A. How do you download a file from the Web using BASH?
B. How do you output the data to a file?

A

A.
curl ‘http://www.url.com’
curl -L ‘http://www.url.com’

B.
curl -L -o filename.html ‘http://www.url.com’

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

A. How would you delete the folder created in BASH?
B. Command -r (recursive)

A

A. rmdir Folder
B. rm -r file_name
C. rm -i file_name (gives warnings)

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

Visualize all the ways to display the contents of a file

A

A. cat (to display the contents) filename.txt
B. nano (to edit the file) filename.txt
C. vim (to edit the file) filename.txt
D. less (to view the file) filename.txt

Scroll down = spacebar | DA
Scroll up = b | UA
Search = /
Exit less = q

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

Visualize how to use vim in BASH

A

vim filename.txt
Press i to enter Insert mode
Press Esc to exit Insert mode
to save changes :w and press Enter
To save and exit :wq and press Enter
Quit vim with :q
To exit without saving :q!
You can exit ZZ

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

Visualize how to run python

A

python3 filename.py

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

Visualize how to open a file in BASH

A

open filename.txt

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

Visualize how to remove multiple folders and files

A

A. rmdir folder1 folder2 folder3
B. rm file1 file2 file3

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

Visualize how to open the files

A

touch filename.txt

36
Q

A. Visualize how to import the os module in Bash
B. Change the directory
C. Create a new file
D. Verify the file is created with list

A

A. import os
B. os.chdir(‘path’)
C. os.system(‘touch filename.txt’)
D. os.listdir()

37
Q

A. Visualize how to use OS to return to the current working directory in BASH.
B. Visualize how to create a new directory given by the path
C. Visualize how to create a directory recursively
D. Remove the file at the specified path
E. Remove the directory at the specified path

A

A. os.getcwd(path)
B. os.mkdir(path)
C. os.makedirs(path)
D. os.remove(path)
E. os.rmdir(path)

38
Q

A. Visualize how to rename a file or directory using OS in BASH from src to dst
B. Visualize how to join one or more path components
C. Visualize how to return True if a path exits or false otherwise
D. Visualize how to execute the command (the string) in a subshell

A

A. os.rename(src, dst)
B. os.path.join(path, *paths)
C. os.path.exists(path)
D. os.system(“command”)

39
Q

What are the ways to exit BASH?

A

exit(), quit()

40
Q

Visualize how to print functions to the terminal

A

A.
x = say_hello()
print(x)

B.
print(say_hello())

41
Q

What will say_hello() print to the screen? and why?

def say_hello():
return “Hello!”
return “Goodbye!”

print(say_hello())

A

It will only return “Hello!” Because functions end after the first return

42
Q

What will animal_count() print to the screen? Why?

def animal_count():
print(“Forty-Two”)
return 42

A

Only Forty-Two will print because we returned the value , but nothing gets done with the returned value.

43
Q

What will more_confused() print to the screen? Why

def more_confused():
2+2

A

None.

A function without a return statement returns None.

44
Q

Visualize how to import modules

A

Create file_name.py and write code
* In terminal, type:
* * python3
* * import file_name

45
Q

Visualize how to use a function directly without the module prefix after importing

A

from file_name import function()
x = function()
print(x)

This imports the function directly and allows you to call it without the module name

46
Q

What is dot notation and how does it work?

A

Dot notation is the period used on a function to denote to python where the module is located. chance.die_roll() is saying to python “call the die_roll() function, which is located in the chance module”.

47
Q

Visualize how to use the newline charachter

A

“For a line break\n”
“Type the newline character\n”

48
Q

Using an if statement, visualize how to count the number of objects in a list inside a function

A

list = len([1,2,3,4,5])

for x in list:
print(x)

the function len() counts lists and characters in strings

49
Q

What module lets you look up characters by emoji names?

A

unicodedata

import unicodedata
unicodedata.lookup(“snake”)

50
Q

visualize how to loop over a list

A

for number in [1,2,3,4]:
print(number)

variable number is assigned each object in list and printed on screen

51
Q

T or F: loops can loop over strings

A

True:
for greeting in “Bonjour!”:
print(greeting)

52
Q

What does it mean to make a directory recursively?

A

It means to create the directory and any necessary parent directories in the specified path if they do not already exist.

ex. home/user/documents/reports/2024
if reports and 2024 do not exist, it will create both folders.
Non-recursive will fail to create 2024 if reports do not exist.

53
Q

Visualize how to create a function and pass parameters

A

def create_function(x, y):
x = 0
for y in string:
if y == variable_targe:
x = x + 1
return x

54
Q

Visualize how to use range in a for loop

A

for side in range(4):
print(side)

55
Q

What does it mean for the function range() to be exclusive

A

It means the number you give is excluded.

range(4) is equivalent to [0,1,2,3]

56
Q

What does each number in range(0, 10, 2) mean?

A

Each number represents a parameter.
0 - the number to start with
10 - the number to stop at
2 - the step size or how large to increment

57
Q

Visualize how to use an f string

A

x = “string”
f”Hello {x}!”

f-strings will convert numerical values into strings
answer = 42
f”The answer is {answer}”
“The answer is 42!”

58
Q

What affect does * have on strings?

A
  • operator performs string repetition.
    For ex.
    n = “2”
    n * 3
    “222”
59
Q

What method is used to check if a f string starts with another?

A

.starts_with()

60
Q

What is the not operation? And visualize it’s use

A

A logical operator used to negate or reverse a boolean value. It returns True if the operand is False, and False if the operand is True.

not x is true if x is false
not x is false if x is true

61
Q

What operations work on all sequence types?

A

the indexing operation
the slicing operation
the len function

62
Q

How can you add 4 to the end of this list numbers = [1,2,3]

A

numbers.append(4)

63
Q

What does .extend() do?

A

Treats its argument as a sequence and adds each item in the sequence to the end of the list. In other words, it adds a sequence of items to a list.

64
Q

How do you sort a list?

A

list = [1,2,3]
list.sort()

65
Q

How do you reverse the order of a list?

A

list = [1,2,3]
list.reverse

66
Q

What is an augmented assignment statement?

A

Shorthand way to update the value of a variable using an operator in Python.

+=, -=, /=, *=

67
Q

How does a while loop works?

A

A while loop runs while some condition is True.

n = 1

while n < 3:
print(n)
n += 1

68
Q

How do you end an infinite loop in the terminal?

A

Ctrl + C

69
Q

What would the impact be on this
my_list = [1,2,3]
my_list.append([4,5,6])
my_list.extend([4,5,6])
my_list.append(“abc”)
my_list.extend(“abc”)

A

my_list.append([4,5,6]) = [1, 2, 3, [4, 5, 6]]
my_list.extend([4,5,6]) = [1, 2, 3, 4, 5, 6]
my_list.append(“abc”) = [1, 2, 3, ‘abc’]
my_list.extend(“abc”) = [1, 2, 3, ‘a’, ‘b’, ‘c’]

70
Q

What does .append() do?

A

Adds its argument as a single item to the end of the list. It only ever adds one item to a list.

71
Q

for word = “cat”
for index in range(len(word)

If we were to add a print statement, what is the difference between print(word) and print(word[index])

A

word refers to the whole string (“cat”)
word[index] extracts the specific character at position index in the string

72
Q

What are the characteristics of a for loop?

A

Comes with a variable
Loops over a sequence of items, assigning to the variable.
Stops when it gets to the end of a sequence

73
Q

What are the characteristics of a while loop?

A

Do not necessarily loop over a sequence
Does not automatically come with or need a variable
Runs while true or until condition is met

74
Q

Visualize a linear search with a for loop

A

def until_dot(string):
for index in range(len(string)):
if string[index] == ‘.’:
# A dot! Return everything up to here.
return string[:index]
else:
# We ran out of string without seeing any dots.
# Return the whole string.
return string

75
Q

Visualize how to do a linear search with a while loop

A

def until_dot(s):
index = 0
while index < len(s) and s[index] != ‘.’:
# No dots yet, keep going.
index += 1
# We either found a dot or ran out of string.
return s[:index]

76
Q

Visualize how to create a while loop that runs infinitely

A

while True:
while True:
print(“I’m trapped.”)
break

77
Q

Visualize how to use the “in” operator for a list and a string

A

‘box’ in ‘big box of trouble’
True
3 in [1, 2, 3, 4]
True

78
Q

Visualize how to use the “not” operator for a list and a string

A

3 not in [1,2,3,4]
False

79
Q

What is pycodestyle and how can you use it?

A

pycodestyle helps you write uniform stylistic Python code.

pip3 install pycodestyle
pycodestyle example.py

80
Q

How can we split very long lines?

A

story = (“Once upon a time there was a very long string that was “
“over 100 characters long and could not all fit on the “
“screen at once.”)

81
Q

Visualize a function that replaces every period in a sentence with a space and “\n”

A

def breakify(string):
return string.replace(“. “, “.\n”)
# Apply the breakify function
output = breakify(intro)
# Print the result
print(output)

82
Q

Visualize how to use join
A. on a list of strings
B. on a path

A

A. words = [“Hello”, “I”, “am”, “Bob”, “the”, “Breakfast”, “Bot”]
# Combine the list of words with spaces between them
sentence = “ “.join(words)
print(sentence)
#### Output: Hello I am Bob the Breakfast Bot
B. # Apply the breakify function
output = breakify(intro)
# Print the result
print(output)

83
Q

When do you need to refractor your code?

A
  • The code is more repetitive than it needs to be
  • The code is overly complex or difficult to read
  • The code is hard to maintain or modify
  • The code is hard to re-use
84
Q

T or F: When defining a function, it should have more than one purpose.

A

False. Refractor functions to have one purpose.

85
Q

T or F: Loops are the only code that is repeatable

A

F: Functions also run repeatable code.

86
Q

How do we access variables from other functions?

A

By passing functions or variables from other fuctions