Python - Introduction Flashcards
In this section, you learn about the fundamentals of Python
What is a variable in Python?
A named reference to a value stored in memory.
Variables store data such as numbers, strings, or lists.
How do you assign the value 10 to a variable named x?
x = 10
The = operator assigns values to variables.
True or False:
Variable names in Python can start with a number
False.
Variable names must start with a letter or an underscore (_).
Which of the following is a valid variable name? 2var, my_var, my-var
my_var
Python variable names cannot start with a number or contain hyphens.
Fill in the blank:
To check a variable’s type, use the ____ function
type()
Example: type(10) → <class ‘int’>.
What is a string in Python?
A sequence of characters enclosed in quotes.
Strings can use single (‘text’) or double (‘“text”’) quotes.
How do you create a multiline string?
Using triple quotes (“"”text””” or ‘'’text’’’).
Example: “"”Hello\nWorld”””.
What does len(“Python”) return?
6
The len() function returns the number of characters in a string.
True or False:
Strings in Python are immutable
True.
You cannot change characters in an existing string.
How do you concatenate two strings a and b?
a + b
Example: “Hello” + “ World” → “Hello World”.
List three numeric types in Python.
- int
- float
- complex
Integers are whole numbers, floats have decimals, and complex numbers have real & imaginary parts.
What is the output of 5 // 2?
2
// performs floor division, discarding the decimal part.
What is the Boolean value of bool(0)?
FALSE
In Python, 0, None, and empty collections evaluate to False.
Which function converts a string “10” into an integer?
int(“10”)
Type conversion functions include int(), float(), and str().
True or False:
3.0 is an integer in python
False.
It is a float (type(3.0) → <class ‘float’>).
What keyword starts an if statement in Python?
if
Example: if x > 0: print(“Positive”).
How do you check if x is equal to 10?
if x == 10:
The == operator checks equality.
True or False:
elif is **optional **in an if-elif-else statement
True.
elif allows multiple conditions but is not required.
Which operator checks if a is not equal to b?
!=
Example: if a != b: print(“Not equal”).
Fill in the blank:
if statements use ____ indentation in Python
Whitespace (4 spaces or a tab)
Python uses indentation instead of {}.
How do you define an empty list?
[] or list()
Lists store multiple elements in an ordered sequence.
What does my_list.append(5) do?
Adds 5 to the end of my_list.
append() modifies the list in place.
True or False:
Lists in Python are mutable
True.
You can change, add, or remove elements.
What does mylist[1:3] return?
A slice of the list from index 1 to 2.
The end index is exclusive.
How do you reverse a list in Python?
my_list.reverse() or my_list[::-1]
.reverse() modifies the list, [::-1] returns a new reversed list.
What loop type runs a block of code multiple times?
for or while loop
Loops iterate over a sequence of items.
How do you define a function in Python?
Using def
Example: def my_function(): pass.
True or False:
Functions must always return a value
False.
Functions without return return None.
How do you exit a loop prematurely?
break
Stops loop execution immediately.
What does continuedo inside a loop?
Skips the current iteration and moves to the next.
Useful for filtering elements in loops.
What is the mean of [2, 4, 6]?
4
Mean = Sum / Count.
True or False:
The median is the middle value of a dataset
True.
If the dataset size is even, the median is the average of the two middle values.
What measure indicates data spread?
Standard deviation.
Higher standard deviation means more variability.
Which function calculates correlation in pandas?
df.corr()
Measures the relationship between variables.
What is the mode?
The most occurring value.
A dataset can have multiple modes.
Which chart type is best for showing trends over time?
Line chart.
Used for continuous data.
What is a common mistake when using a pie chart?
Using too many slices.
Makes it difficult to compare proportions.
True or False:
Bar charts can represent categorical data
True.
They compare different categories.
How do you label a matplotlib plot?
plt.xlabel() and plt.ylabel()
Ensures clarity in visualizations.
What is the primary purpose of a histogram?
To show the distribution of numerical data.
Useful for identifying skewness and outliers.