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
Q

How do you reverse a list in Python?

A

my_list.reverse() or my_list[::-1]

.reverse() modifies the list, [::-1] returns a new reversed list.

26
Q

What loop type runs a block of code multiple times?

A

for or while loop

Loops iterate over a sequence of items.

27
Q

How do you define a function in Python?

A

Using def

Example: def my_function(): pass.

28
Q

True or False:

Functions must always return a value

A

False.

Functions without return return None.

29
Q

How do you exit a loop prematurely?

A

break

Stops loop execution immediately.

30
Q

What does continuedo inside a loop?

A

Skips the current iteration and moves to the next.

Useful for filtering elements in loops.

31
Q

What is the mean of [2, 4, 6]?

A

4

Mean = Sum / Count.

32
Q

True or False:

The median is the middle value of a dataset

A

True.

If the dataset size is even, the median is the average of the two middle values.

33
Q

What measure indicates data spread?

A

Standard deviation.

Higher standard deviation means more variability.

34
Q

Which function calculates correlation in pandas?

A

df.corr()

Measures the relationship between variables.

35
Q

What is the mode?

A

The most occurring value.

A dataset can have multiple modes.

36
Q

Which chart type is best for showing trends over time?

A

Line chart.

Used for continuous data.

37
Q

What is a common mistake when using a pie chart?

A

Using too many slices.

Makes it difficult to compare proportions.

38
Q

True or False:

Bar charts can represent categorical data

A

True.

They compare different categories.

39
Q

How do you label a matplotlib plot?

A

plt.xlabel() and plt.ylabel()

Ensures clarity in visualizations.

40
Q

What is the primary purpose of a histogram?

A

To show the distribution of numerical data.

Useful for identifying skewness and outliers.