Python - Introduction Flashcards

In this section, you learn about the fundamentals of Python

1
Q

What is a variable in Python?

A

A named reference to a value stored in memory.

Variables store data such as numbers, strings, or lists.

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

How do you assign the value 10 to a variable named x?

A

x = 10

The = operator assigns values to variables.

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

True or False:

Variable names in Python can start with a number

A

False.

Variable names must start with a letter or an underscore (_).

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

Which of the following is a valid variable name? 2var, my_var, my-var

A

my_var

Python variable names cannot start with a number or contain hyphens.

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

Fill in the blank:

To check a variable’s type, use the ____ function

A

type()

Example: type(10) → <class ‘int’>.

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

What is a string in Python?

A

A sequence of characters enclosed in quotes.

Strings can use single (‘text’) or double (‘“text”’) quotes.

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

How do you create a multiline string?

A

Using triple quotes (“"”text””” or ‘'’text’’’).

Example: “"”Hello\nWorld”””.

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

What does len(“Python”) return?

A

6

The len() function returns the number of characters in a string.

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

True or False:

Strings in Python are immutable

A

True.

You cannot change characters in an existing string.

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

How do you concatenate two strings a and b?

A

a + b

Example: “Hello” + “ World” → “Hello World”.

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

List three numeric types in Python.

A
  • int
  • float
  • complex

Integers are whole numbers, floats have decimals, and complex numbers have real & imaginary parts.

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

What is the output of 5 // 2?

A

2

// performs floor division, discarding the decimal part.

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

What is the Boolean value of bool(0)?

A

FALSE

In Python, 0, None, and empty collections evaluate to False.

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

Which function converts a string “10” into an integer?

A

int(“10”)

Type conversion functions include int(), float(), and str().

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

True or False:

3.0 is an integer in python

A

False.

It is a float (type(3.0) → <class ‘float’>).

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

What keyword starts an if statement in Python?

A

if

Example: if x > 0: print(“Positive”).

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

How do you check if x is equal to 10?

A

if x == 10:

The == operator checks equality.

18
Q

True or False:

elif is **optional **in an if-elif-else statement

A

True.

elif allows multiple conditions but is not required.

19
Q

Which operator checks if a is not equal to b?

A

!=

Example: if a != b: print(“Not equal”).

20
Q

Fill in the blank:

if statements use ____ indentation in Python

A

Whitespace (4 spaces or a tab)

Python uses indentation instead of {}.

21
Q

How do you define an empty list?

A

[] or list()

Lists store multiple elements in an ordered sequence.

22
Q

What does my_list.append(5) do?

A

Adds 5 to the end of my_list.

append() modifies the list in place.

23
Q

True or False:

Lists in Python are mutable

A

True.

You can change, add, or remove elements.

24
Q

What does mylist[1:3] return?

A

A slice of the list from index 1 to 2.

The end index is exclusive.

25
How do you reverse a list in Python?
**my_list.reverse()** or **my_list[::-1]** ## Footnote .reverse() modifies the list, [::-1] returns a new reversed list.
26
What loop type runs a block of code **multiple times**?
for or while loop ## Footnote Loops iterate over a sequence of items.
27
How do you define a function in Python?
Using **def** ## Footnote Example: def my_function(): pass.
28
# True or False: Functions must always return a value
**False.** ## Footnote Functions without return return None.
29
How do you exit a loop prematurely?
**break** ## Footnote Stops loop execution immediately.
30
What does **continue**do inside a loop?
Skips the current iteration and moves to the next. ## Footnote Useful for filtering elements in loops.
31
What is the **mean** of [2, 4, 6]?
**4** ## Footnote Mean = Sum / Count.
32
# True or False: The median is the middle value of a dataset
**True.** ## Footnote If the dataset size is even, the median is the average of the two middle values.
33
What measure indicates data **spread**?
Standard deviation. ## Footnote Higher standard deviation means more variability.
34
Which function calculates **correlation** in pandas?
**df.corr()** ## Footnote Measures the relationship between variables.
35
What is the **mode**?
The most occurring value. ## Footnote A dataset can have multiple modes.
36
Which chart type is best for showing **trends** over time?
Line chart. ## Footnote Used for continuous data.
37
What is a common mistake when using a **pie chart**?
Using too many slices. ## Footnote Makes it difficult to compare proportions.
38
# True or False: **Bar charts** can represent categorical data
**True.** ## Footnote They compare different categories.
39
How do you label a matplotlib plot?
**plt.xlabel()** and **plt.ylabel()** ## Footnote Ensures clarity in visualizations.
40
What is the primary purpose of a **histogram**?
To show the **distribution** of **numerical data**. ## Footnote Useful for identifying skewness and outliers.