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.