Intro to py Flashcards

(18 cards)

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

What is a variable in Python?

A

A variable is a name that refers to a value stored in memory.

```python
x = 42
name = “"”Alice”””
~~~

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

How does assignment work?

A

= binds the object on the right to the variable name on the left. Re‑assigning changes the reference.

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

What is dynamic typing?

A

A variable can refer to objects of different types at runtime.

```python
n = 10 # int
n = “"”ten””” # now str
~~~

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

Which built-in function shows an object’s type?

A

type(obj) returns the class of the object.

```python
print(type(3.14)) # <class ‘float’>
~~~

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

List the core arithmetic operators introduced in Chapter 2.

A

+ - * / ** // %

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

Which operator has the highest arithmetic precedence?

A

Exponentiation **; parentheses override all precedence rules.

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

Show an example mixing // and %.

A

```python
minutes = 143
hours = minutes // 60 # 2
left = minutes % 60 # 23
~~~

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

How do you print text and variables together?

A

Use commas in print() or f‑strings.

```python
age = 30
print(““Age:””, age) # comma style
print(f”“Age: {age}””) # f‑string
~~~

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

What are triple-quoted strings used for?

A

Multiline text or docstrings.

```python
doc = “””"”Line 1
Line 2”””””

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

How do you read keyboard input?

A

input(prompt) returns a str; convert if needed.

```python
age = int(input(““Age? “”))
~~~

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

Write an if–else that checks if a number is positive.

A

```python
n = int(input(““Number: “”))
if n > 0:
print(““Positive””)
else:
print(““Zero or negative””)
~~~

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

List all comparison operators covered.

A

== != < <= > >=

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

What does indentation signify in Python?

A

Indentation defines code blocks; all lines in the same block must align consistently.

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

Give a one-line conditional expression (ternary).

A

```python
parity = ““even”” if n % 2 == 0 else ““odd””
~~~

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

Which standard‑library module provides basic statistics?

A

statistics.

```python
from statistics import mean, median
nums = [10, 3, 8]
print(mean(nums)) # 7.0
print(median(nums)) # 8
~~~

17
Q

Coding Exercise ① – simple calculator

A

Write a script that asks for two numbers and prints their sum, difference, product, and quotient.

Sample solution:

```python
a = float(input(““First number: “”))
b = float(input(““Second number: “”))
print(““Sum:””, a + b)
print(““Difference:””, a - b)
print(““Product:””, a * b)
if b != 0:
print(““Quotient:””, a / b)
else:
print(““Cannot divide by zero””)
~~~

18
Q

Coding Exercise ② – temperature classifier

A

Ask the user for a Celsius temperature. Print “Cold” if below 10 °C, “Warm” if 10–25 °C, otherwise “Hot.”

Sample solution:

```python
temp = float(input(““Temperature °C: “”))
if temp < 10:
print(““Cold””)
elif temp <= 25:
print(““Warm””)
else:
print(““Hot””)
~~~