Computing Flashcards
(19 cards)
name the 4 variable types and the data typ they hold add examples
integer (int): Whole numbers (e.g., 5, -2)
float (float): Decimal numbers (e.g., 3.14, -0.5)
string (str): Strings/text (e.g., “hello”)
Boolean (bool): Boolean values (True, False)
name 3 rules for naming variables
Must not start with a digit
No spaces or hyphens
Can include underscores (_)
witch variable names are valid and which are not:
-full_name
-1st_place
-my var
-full-name
Correct: full_name
Incorrect: 1st_place, my var, full-name
name the 2 types of loops and when they are used
For Loops:
Used when the number of iterations is known
While Loops:
Used when repetitions depend on a condition:
use a for loop and write a code that prints out “2, 3, 4, 5, 6”
Outputs: “2, 3, 4, 5, 6”
for i in range(2, 7):
print(i)
# Outputs: 2, 3, 4, 5, 6
use a while loop ad write a code that prints out “0, 1, 2”
Outputs: 0, 1, 2
i = 0
while i < 3:
print(i)
i += 1
# Outputs: 0, 1, 2
how to define a list
square brackets: (e.g. my_list = [1, 2, 3])
what index do list variable start at and so how would you access “2” in the list 1, 2, 3
Index starts at 0
my_list[1]
use the append function to add 4 to the list 1, 2, 3
nums = [1,2,3]
print(nums) #output = 1,2,3
nums.append(4) → [1,2,3,4]
print(nums) #output = 1,2,3,4
what could you use to find the length of a list
len(nums) gives number of elements
what is a function and give an example of it
Function: A block of code that may or may not return a value using return. Functions could take parameters
def add(a, b):
return a + b
result = add(3, 4)
print(result) # Output: 7
define procedure and give an example of it
Procedure: A block of code that performs an action but does not return a value. can take parameters
def greet(name):
print(f”Hello, {name}!”)
greet(“Tyme”)
define conditions in python and give an example
Conditions are codes that check if something’s true or not, so the program can decide what to do. using if, elif, and else.
age = 18
if age >= 18:
print(“You are an adult.”)
else:
print(“You are a minor.”)
what does == and != mean
”==”: equal to
“!= “: not equal to
what is a comment for in python and give an example
A comment is a code that Python ignores, it’s for humans to read so they understand the code better
” #this is a comment “
what is the ZeroDivisionError in python and give example
def divide(10, 0):
return 10 / 0
divide(10, 0) # Causes ZeroDivisionError
what happens if you multiply a string in python
The string repeats that many times.
x = “7”
y = 2
print(x * y) # Output: “77”
how do you edit items in a list
a = [10, 20, 30]
for item in a:
print(item + 5)
# output 15, 25, 35
how do you find the total of a range of numbers in python
total = 0
for i in range(1, 4):
total += i
print(total) # Output: 6