Programming Flashcards

1
Q

How do you get user input in Python?

A

quantity = input()

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

How do you convert an input string to an integer?

A

quantity = int(input(variable))

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

How do you take two separate user inputs?

A

num1 = input()
num2 = input()

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

How do you convert an input string to a float?

A

quantity_input = input()
quantity = float(quantity_input)

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

How do you print a string in Python?

A

print(“This is a string”)

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

How do you print a variable?

A

amount = 5
print(amount)

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

How do you print two strings separated by a space?

A

first_name = ‘Sofia’
last_name = ‘Petra’
print(first_name , last_name)

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

How do you print multiple values separated by commas?

A

num1 = 23
num2 = 5
num3 = -7
print((num1) + ‘,’ +(num2) + ‘,’ +(num3))

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

How do you find the length of a string?

A

my_string = ‘crocodile’

my_string_length = len(my_string)

print(my_string_length)

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

How do you join two strings together?

A

my_string = ‘crocodile’
new_string = ‘ccc’ + my_string
print(new_string)

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

How do you access the different characters of a string?

A

my_string = ‘crocodile’

first_letter = my_string[0]

sixth_letter = my_string[5]

print(first_letter)

print(sixth_letter)

Strings always start from 0

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

How do you convert a string to uppercase

A

my_string = ‘crocodile’
print(my_string.upper())

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

How do you convert a string to lowercase

A

my_string = ‘DESK’
print(my_string.lower())

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

How do you perform basic mathematical operations in Python?

A

a = 7
b = 2

addition = a + b
subtraction = a - b
division = a / b
multiplication = a * b

print(addition, subtraction, division, multiplication)

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

How do you round a number to certain decimal places?

A

pi = 3.14159
pi_rounded = round(pi, 3)
print(pi_rounded)

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

How do you compare two numbers using relational operators?

A

a = 7
b = 2

print(a == b) # Equal to
print(a > b) # Greater than
print(a < b) # Less than
print(a >= b) # Greater than or equal to
print(a <= b) # Less than or equal to
print(a != b) # Not equal to

17
Q

How do you use a for loop with a range?

A

for i in range(3):
print(‘Hello’)

Hello
Hello
Hello

18
Q

How do you iterate over each character in a string?

A

motto = ‘Be kind’
for character in motto:
print(character)

B
E

K
I
N
D

19
Q

How do use a for loop to repeat but on the same line

A

for i in range(3):
print(‘Hello’, end=’ ‘)

Hello Hello Hello

20
Q

How do you print values on the same line with a comma separator?

A

for i in range(3):
print(‘Hello’, end=’,’)

Hello,Hello,Hello,

21
Q

How do you print the loop variable in a for loop?

A

for i in range(3):
print(i)

0
1
2

22
Q

How do you print a range of numbers

A

for i in range(2, 5):
print(i)

2
3
4

23
Q

How do you print numbers in a range but with intervals

A

for i in range(3, 10, 2):
print(i)

3
5
7
9

24
Q

How do you count numbers down, with intervals

A

for i in range(10, 0, -2):
print(i)

10
8
6
4
2

25
How to turn consonants into lower case / upper case
text = "hello world" vowels = "aeiou" new_text = "" for char in text: if char in vowels: new_text += char else: new_text += char.upper() # Convert consonants to uppercase print(new_text)
26
How to turn vowles into upper / lower case
text = "hello world" vowels = "aeiou" new_text = "" for char in text: if char in vowels: new_text += char else: new_text += char.upper() print(new_text)
27
How to change character in a certain position to upper / lowercase
text = "hello world" new_text = "" for i in range(len(text)): if i == 2: # Third character (index 2) new_text += text[i].upper() else: new_text += text[i] print(new_text) ##footnote heLlo world