For & while Flashcards

1
Q

[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]

k many times
int(input(‘k: ‘)

A

n = 1

for i in range(int(input(‘k:’))):
print(list(i for i in range(0, n)))
n += 1

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

Print:[0,1,4,9,16] with while loop (predefined variable)

A

n = int(input(“Provide an integer: “))

i = 0

while i < n :
print(i**2)
i += 1

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

Print:[0,1,4,9,16] with while loop (predefined variable + result)

A

n = int(input(“Provide an integer”))

i = 0

while i < n:
result =i **2
print(result)
i += 1

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

Print:[0,1,4,9,16] with for loop

A

number = (int(input(“Provide an integer”))

for i in range(1, number + 1):
square = i ** 2
print(square)

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

Print: [25] as 5 to the 5th with while loop

A

n = 6
i = 1

while i <= n:
result =i **2
i += 1

print(result)

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

Print:[49] as 7 to the 7th with for loop

A

n = 7

for i in range(1, n + 1):
square = i ** 2

print(square)

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

Print: while loop
The sum of the numbers starting from 1 to n is: 1
The sum of the numbers starting from 1 to n is: 3
The sum of the numbers starting from 1 to n is: 6
The sum of the numbers starting from 1 to n is: 10
The sum of the numbers starting from 1 to n is: 15
The sum of the numbers starting from 1 to n is: 21
The sum of the numbers starting from 1 to n is: 28
The sum of the numbers starting from 1 to n is: 36
The sum of the numbers starting from 1 to n is: 45

A

n = 9

result = 0
i = 1

while i <= n:
result += i
i += 1

print(f"The sum of the numbers starting from 1 to n is: {result} ")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Print:
The sum of the numbers starting from 1 to n is: 45

A

n = 9

result = 0
i = 1

while i <= n:
result += i
i += 1

print(f”The sum of the numbers starting from 1 to n is: {result} “)

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

Print: while : FACTORIAL
The sum of the numbers starting from 1 to n is: 120

A

n = 5

result = 1
i = 1

while i <= n:
result *= i
i += 1

print(f”The sum of the numbers starting from 1 to n is: {result} “)

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

Print: row = 5
1
1
2
1
2
3
1
2
3
4
1
2
3
4
5

A

row = 5

for i in range(1, row + 1, 1):
for j in range(1, i + 1):
print(j)

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

Print: row = 5
1
1 2
1 2 3
1 2 3 4
12 3 4 5

A

row = 5

for i in range(1, row + 1, 1):
for j in range(1, i + 1):
print(j, end=’ ‘)
print(“”)

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

Print: row = 5
1 1 2 1 2 3 1 2 3 4 1 2 3 45

A

row = 5

for i in range(1, row + 1, 1):
for j in range(1, i + 1):
print(j, end=’ ‘)

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

Print: row = 5
5
4
3
2
1
4
3
2
1
3
2
1
2
1
1

A

row = 5

for i in range(0, row + 1):
for j in range(row-i, 0, -1):
print(j, end=’ ‘)
print(‘ ‘)

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

Print: row = 5

5 4 3 21
4 3 2 1
3 2 1
2 1
1

A

row = 5

for i in range(0, row + 1):
for j in range(row-i, 0, -1):
print(j, end=’ ‘)
print(‘ ‘)

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

Print: row = 5

5 4 3 2 1 4 3 2 1 3 2 1 2 11

A

row = 5

for i in range(0, row + 1):
for j in range(row-i, 0, -1):
print(j, end=’ ‘)

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

Print:

1
22
333
4444
55555
666666
7777777
88888888
999999999

A

for i in range(1, 10):
for j in range(0, i):
print(i, end=’ ‘)
print(‘ ‘)

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

Print:

1
22
333
4444
55555
666666
7777777
88888888
999999999

A

for i in range(1,10):
print(”%d” % i * i)

18
Q

Print:
*
**
*
**
**
*
**
***
**

***
**
**

A

for i in range(1,10):
print(”*” * i)

19
Q

Print Fibonacci sequence to 34: with for loop
0, 1, 1, 2, 3, 5, 8, 13, 21,34

A

num1 = 0
num2 = 1

print(‘Fibonacci sequence: ‘)

for i in range(10):
print(num1, end=’ ‘)
res =num1 + num2

num1 = num2
num2 = res
20
Q

Print:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana

A

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

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

21
Q

Print(ls) ; n1=2, n2=14
[2,4,6,8,10,12,14]

A

n1 = 2
n2 = 14

ls = []
for i in range(n1, n2 + 1):
if i % 2== 0:
ls.append(i)
print(ls)

22
Q

Print(ls) ; n1=2, n2=14

[2]
[2, 4]
[2, 4, 6]
[2, 4, 6, 8]
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10, 12]
[2, 4, 6, 8, 10, 12, 14]

A

n1 = 2
n2 = 14

ls = []
for i in range(n1, n2 + 1):
if i % 2== 0:
ls.append(i)
print(ls)

23
Q

Print(i) ; n1=2, n2=14,
2 4 6 8 10 12 14

A

n1 = 2
n2 = 14

ls = []
for i in range(n1, n2 + 1):
if i % 2== 0:
ls.append(i)
print(i, end=’ ‘)

24
Q

Print(i) ; n1=2, n2=14,
2 3 4 5 6 7 8 9 10 11 12 13 14

A

n1 = 2
n2 = 14

ls = []
for i in range(n1, n2 + 1):
if i % 2== 0:
ls.append(i)
print(i, end=’ ‘)

25
Q

Print: 0,1,2,3,4,5,6,7,8,9,10 with while

A

i = 0

while i < 11:
print(i, end=’ ‘)
i += 1

26
Q

Print: 1,2,3,4,5,6,7,8,9,10 with while

A

i = 0

while i < 11:
i += 1
print(i, end=’ ‘)

27
Q

Print: for - Res:2
2
4
6
8
10
12
14
16
18
20

A

result = 2

for i in range(10):
k = result * i
print(k)
Nullatol kezdi - miért?

28
Q

Print: Factorial with for loop
720 as factorial of 6 (num = 6)

A

num = 6
result = 1

for i in range(1, num + 1):
result = result *i
print(result)

29
Q

Print: 22 as sum of 1-6 (num = 6)

A

num = 6
result = 1

for i in range(1, num + 1):
result = result + i
print(result)

30
Q

Print if given number if PRIME

A

num = int(input(“Provide an integer: “))

if num == 1:
print(num, “is not a prime number”)
elif num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,”is not a prime number”)
break
else:
print(num,”is a prime number”)

else:
    print(num,"is not a prime number")
31
Q

Print:
[1,3,6,10,15,21,28]

A

n = 7

result = 0

for i in range(1, n + 1):
result += i
print(result)

32
Q

Print: 28 - as sum of 0-7

A

n = 7

result = 0

for i in range(1, n + 1):
result += i
print(result)

33
Q

Provide an integer: 8
[6, 4, 6, 7, 8]

A

ls = []

for _ in range(5):
n = int(input(“Adj meg egy számot: “))
ls.append(n)
print(ls)

34
Q

5 times random number between 0 - 20 in a list

A

import random

ls = []

for i in range(5):
n = random.randint(0,20)
ls.append(n)
print(ls)

35
Q

Print: [‘change’, ‘change’, ‘change’] - as per the len of a precreated list

A

ls = [7,23,2]

for i in range(len(ls)):
ls[i] = “change”
print(ls)

36
Q

Print: 1 # 2 # 3 # 4 # 5

A

print(1, 2, 3, 4, 5, sep=” # “)

37
Q

1
2
3

1
2
3
4

1
2
3
4
5

A

row = 5

for i in range(3, row+1):
for j in range(1, i+1):
print(j)
print(‘ ‘)

38
Q

Fibonacci list until a given number:

A

num1 = 0
num2 = 1

given = int(input(‘Provide the number until you would like to see the Fibonacci list: ‘))

while num1 <= given:
print(num1)
num3 = num1 + num2
num1 = num2
num2 = num3

39
Q

for loop:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

A

for x in range(3):
for y in range (1, 10):
print(y, end=’ ‘)
print(‘ ‘)

40
Q

Given: rows, columns, symbol.
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >

A

rows = int(input(‘Enter no of rows: ‘))
columns = int(input(‘Enter no of columns: ‘))
symbol = input(‘Enter a symbol to use: ‘)

for x in range(rows):
for y in range (columns):
print(symbol, end=’ ‘)
print(‘ ‘)

41
Q

Check if num 10 is prime

A

num = 10

for i in range(2, num):
if num % i:
print(‘Not prime’)
break
else:
print(‘Prime’)

42
Q

for else - if divisible by 5

nums = [10,16,18,21,25]

A

nums = [10,16,18,21,25]

for num in nums:
if num % 5 == 0:
print(num)
break

else:
print(‘not found’)