Loops and Functions Flashcards

1
Q

Write a for loop that prints out the squares of the numbers 1 through 10.

A

for i in range(1, 11):
print(i ** 2)

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

Write a very basic function that calculates the squarte of a number

A

def square_number(x):
return x**2

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

Loop through the letters in the word “banana”:

A

for x in “banana”:
print(x)

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

Exit the loop when x is “banana”:

fruits = [“apple”, “banana”, “cherry”]

A

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print(x)
if x == “banana”:
break

  • returns “apple” and “banana”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Exit the loop when x is “banana”, but this time the break comes before the print:

fruits = [“apple”, “banana”, “cherry”]

A

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana”:
break
print(x)

-returns just “apple”

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

Do not print banana:

fruits = [“apple”, “banana”, “cherry”]

A

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana”:
continue
print(x)

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

what does range() do?

A

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

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

for x in range(6):
print(x)

Is range(6) the values of 0 to 6 or 0 to 5?

A

0-5

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

for x in range():
print(x)

convert to make the loop start at 2 and end at 5

A

for x in range(2, 6):
print(x)

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

for x in range():
print(x)

convert to make loop start at 2, end at 30, but loop in increments of 3

A

for x in range(2, 30, 3):
print(x)

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

Print all numbers from 0 to 5, and print a message when the loop has ended:

A

for x in range(6):
print(x)
else:
print(“Finally finished!”)

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

for x in range(6):
if x == 3: break
print(x)
else:
print(“Finally finished!”)

is the else block executed?

A

If the loop breaks, the else block is not executed.

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

What’s a nested loop?

A

A nested loop is a loop inside a loop.

The “inner loop” will be executed one time for each iteration of the “outer loop”:

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

Print each adjective for every fruit:

adj = [“red”, “big”, “tasty”]
fruits = [“apple”, “banana”, “cherry”]

A

for x in adj:
for y in fruits:
print(x, y)

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

Define a function (my_function) that prints the first name from this list + the last name, Refsnes:
Emily, Tobias, Linus

A

def my_function(fname):
print(fname + “ Refsnes”)

my_function(“Emil”)
my_function(“Tobias”)
my_function(“Linus”)

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

what do you do if you do know how many arguments will be passed into your function, say, 2. What’s an example?

A

def my_function(fname, lname):
print(fname + “ “ + lname)

my_function(“Emil”, “Refsnes”)

17
Q

what’s an example of not passing the right number of arguments through the function, say trying to pass 1 through a function that expects 2 arguments?

A

def my_function(fname, lname):
print(fname + “ “ + lname)

my_function(“Emil”)

18
Q

what do you do if you do not know how many arguments will be passed into your function?

A

Add a * before the parameter name in the function definition.

def my_function(*kids):
print(“The youngest child is “ + kids[2])

my_function(“Emil”, “Tobias”, “Linus”)

19
Q

with the following countries, define a function that prints “I am from” + country

sweden, india, norway, brazil

A

def my_function(country = “Norway”):
print(“I am from “ + country)

my_function(“Sweden”)
my_function(“India”)
my_function()
my_function(“Brazil”)

20
Q

define a function that multiplies the following numbers by 5

A

def my_function(x):
return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))

21
Q

How do you specify that a function can have ONLY positional arguments, or ONLY keyword arguments

A

To specify that a function can have only positional arguments, add , / after the arguments:

22
Q
A
23
Q

for i in range(1, 11):
print(i ** 2)

Modify this code so that it prints out the squares of the even integers between 1 and 10

A

for i in range(1, 11):
if i % 2 == 0:
print(i ** 2)

or

for number in range(11):
print (f’orginal: {number} transformed number”: {number**2}’)

24
Q

get data into Python, write a query and execute it using the sqlalchemy library.

A

database_name = ‘lahman’ # Fill this in with your lahman database name
connection_string = f”postgresql://postgres:postgres@localhost:5432/{database_name}”
engine = create_engine(connection_string)

25
Q

Write a function named collatz that will take as input a number. If that number is even, your function should divide it by two and return the result. If the number is odd, it should triple the number and add one and then return the result.

A

def collatz(x):
if x % 2:
return x/2
else:
return x*3+1

print(collatz(6))

26
Q

how is range() helpful?

A

range can be helpful when writing for loops if you want to iterate through a set of consecutive numbers.

27
Q

if we wanted to sum the first 100 positive integers we could do this using the accumulator pattern.

A

total_sum = 0
for number in range(1, 101):
total_sum += number

print(f’The total sum is {total_sum}’)

28
Q

Find the sum of the squares of all even integers between 2 and 1000.

A

total_sum=0
for d in range(2, 1001, 2):
total_sum +=(d**2)

total_sum

29
Q

write a very basic function to calculate the square of a number

A

def square_number(x):
return x**2

30
Q

Write a function get_teams that will take as input a year and which returns the team stats (from the teams table) for the input year.

A

def get_teams(year):
return teams.loc[teams[‘yearid’] == year]

–then this returns all from 2009:

_2009 = get_teams(2009)
_2009

31
Q
A
32
Q
A