Computing Flashcards

(19 cards)

1
Q

name the 4 variable types and the data typ they hold add examples

A

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)

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

name 3 rules for naming variables

A

Must not start with a digit

No spaces or hyphens

Can include underscores (_)

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

witch variable names are valid and which are not:
-full_name
-1st_place
-my var
-full-name

A

Correct: full_name

Incorrect: 1st_place, my var, full-name

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

name the 2 types of loops and when they are used

A

For Loops:
Used when the number of iterations is known

While Loops:
Used when repetitions depend on a condition:

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

use a for loop and write a code that prints out “2, 3, 4, 5, 6”

A

Outputs: “2, 3, 4, 5, 6”

for i in range(2, 7):
print(i)
# Outputs: 2, 3, 4, 5, 6

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

use a while loop ad write a code that prints out “0, 1, 2”

A

Outputs: 0, 1, 2

i = 0
while i < 3:
print(i)
i += 1
# Outputs: 0, 1, 2

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

how to define a list

A

square brackets: (e.g. my_list = [1, 2, 3])

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

what index do list variable start at and so how would you access “2” in the list 1, 2, 3

A

Index starts at 0
my_list[1]

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

use the append function to add 4 to the list 1, 2, 3

A

nums = [1,2,3]
print(nums) #output = 1,2,3
nums.append(4) → [1,2,3,4]
print(nums) #output = 1,2,3,4

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

what could you use to find the length of a list

A

len(nums) gives number of elements

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

what is a function and give an example of it

A

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

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

define procedure and give an example of it

A

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”)

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

define conditions in python and give an example

A

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.”)

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

what does == and != mean

A

”==”: equal to
“!= “: not equal to

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

what is a comment for in python and give an example

A

A comment is a code that Python ignores, it’s for humans to read so they understand the code better

” #this is a comment “

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

what is the ZeroDivisionError in python and give example

A

def divide(10, 0):
return 10 / 0

divide(10, 0) # Causes ZeroDivisionError

16
Q

what happens if you multiply a string in python

A

The string repeats that many times.

x = “7”
y = 2
print(x * y) # Output: “77”

16
Q

how do you edit items in a list

A

a = [10, 20, 30]
for item in a:
print(item + 5)
# output 15, 25, 35

17
Q

how do you find the total of a range of numbers in python

A

total = 0
for i in range(1, 4):
total += i
print(total) # Output: 6