Beginning Python Flashcards
Foundations of Python
Visualize how to import a library
import library
Visualize a for loop that uses a list and describe what each part is doing in the for loop
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.
Visualize a list
list = [a,b,c,d]
Visualize a nested loop
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
What is a compound statement
A compound statement contains other statements inside of it.
like a for loop
They change the the control flow of information
Visualize how to use range in a for loop
For x in range(4):
print(x)
What is a method
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.
What is a parameter?
A parameter is a variable that is defined in a function definition.
def spiral(sides)
Sides is the variable
What is an argument?
An argument is an input that we pass to a function.
Spiral(100). 100 is the argument.
What is the scope of a variable that is passed inside a function?
It is a local scope, and can only be used inside the function.
What is the scope of a variable passed outside of a function
It is a global scope and can be used both inside functions and outside functions.
What is a conditional statement?
A conditional statement tells python to run only when a condition is met.
Visualize a conditional statement
x = t.Turtle()
if x == 1:
x.color(‘blue’)
else:
x.color(‘yellow’)
What is the modulo operator %?
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
What happens if a % b and b is bigger?
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
Visualize how to use % in a for loop
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
Visualize how to use rand.choice
import random
A.
color = random.choice([‘red’,’blue’,’green’])
B.
colors = [‘red’,’blue’,’green’]
color = random.choice(colors)
Visualize how to use to use random.randint
import random
roll_die = random.randint(1,6)
Visualize all of the operators
a == b, a < b, a > b, a <= b, a >= b, a != b
How does an elif work in an if statement? Visualize how to use an elif
Nesting if else statements is the same as using elif
if mood == “happy”:
color = “pink”
elif mood == “calm”:
color = “lightBlue”
else:
color = “red”
What is Bash?
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
What do you type for BASH to recall your last command?
!!
Visualize how to create a variable in bash
x=100
echo $x
100
How do you list the contents of the current directory in BASH?
ls
How do you change directory in BASH? How do you go up one directory?
cd
cd ..
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. mkdir
B. mv
C.
mkdir Photos
mv *.jpg Photos
mkdir Animations
mv *.gif Animations
D. ls
How do you rename files in BASH?
mv old_filename.txt new_filename.txt
A. How do you download a file from the Web using BASH?
B. How do you output the data to a file?
A.
curl ‘http://www.url.com’
curl -L ‘http://www.url.com’
B.
curl -L -o filename.html ‘http://www.url.com’
A. How would you delete the folder created in BASH?
B. Command -r (recursive)
A. rmdir Folder
B. rm -r file_name
C. rm -i file_name (gives warnings)
Visualize all the ways to display the contents of a file
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
Visualize how to use vim in BASH
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
Visualize how to run python
python3 filename.py
Visualize how to open a file in BASH
open filename.txt
Visualize how to remove multiple folders and files
A. rmdir folder1 folder2 folder3
B. rm file1 file2 file3