Programming Flashcards
How do you get user input in Python?
quantity = input()
How do you convert an input string to an integer?
quantity = int(input(variable))
How do you take two separate user inputs?
num1 = input()
num2 = input()
How do you convert an input string to a float?
quantity_input = input()
quantity = float(quantity_input)
How do you print a string in Python?
print(“This is a string”)
How do you print a variable?
amount = 5
print(amount)
How do you print two strings separated by a space?
first_name = ‘Sofia’
last_name = ‘Petra’
print(first_name , last_name)
How do you print multiple values separated by commas?
num1 = 23
num2 = 5
num3 = -7
print((num1) + ‘,’ +(num2) + ‘,’ +(num3))
How do you find the length of a string?
my_string = ‘crocodile’
my_string_length = len(my_string)
print(my_string_length)
How do you join two strings together?
my_string = ‘crocodile’
new_string = ‘ccc’ + my_string
print(new_string)
How do you access the different characters of a string?
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 do you convert a string to uppercase
my_string = ‘crocodile’
print(my_string.upper())
How do you convert a string to lowercase
my_string = ‘DESK’
print(my_string.lower())
How do you perform basic mathematical operations in Python?
a = 7
b = 2
addition = a + b
subtraction = a - b
division = a / b
multiplication = a * b
print(addition, subtraction, division, multiplication)
How do you round a number to certain decimal places?
pi = 3.14159
pi_rounded = round(pi, 3)
print(pi_rounded)
How do you compare two numbers using relational operators?
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
How do you use a for loop with a range?
for i in range(3):
print(‘Hello’)
Hello
Hello
Hello
How do you iterate over each character in a string?
motto = ‘Be kind’
for character in motto:
print(character)
B
E
K
I
N
D
How do use a for loop to repeat but on the same line
for i in range(3):
print(‘Hello’, end=’ ‘)
Hello Hello Hello
How do you print values on the same line with a comma separator?
for i in range(3):
print(‘Hello’, end=’,’)
Hello,Hello,Hello,
How do you print the loop variable in a for loop?
for i in range(3):
print(i)
0
1
2
How do you print a range of numbers
for i in range(2, 5):
print(i)
2
3
4
How do you print numbers in a range but with intervals
for i in range(3, 10, 2):
print(i)
3
5
7
9
How do you count numbers down, with intervals
for i in range(10, 0, -2):
print(i)
10
8
6
4
2