Fundamentals of Python Syntax and Data Types
Welcome to this comprehensive module on Python basics. Whether you are a beginner or need a quick refresher, this course will walk you through the core concepts that appear in many…

What is the result of evaluating `type(x / 2)` when `x = 10`?
Which of the following is a valid identifier name in Python?
What does the expression `print(True or False)` output?
Which function returns the number of elements in a list or tuple?
What will be printed by the following code? ```python my_set = {1, 2, 3} my_set.add(2) print(my_set) ```
When accessing a dictionary with a non‑existent key, which outcome occurs?
What is the output of the following loop? ```python for i in range(3): print(i) ```
Which slicing expression returns the substring `'def'` from the string `alphabet = "abcdefg"`?
What does the expression `x = 10 < 5` evaluate to, and what is printed?
Fundamentals of Python Syntax and Data Types
Welcome to this comprehensive module on Python basics. Whether you are a beginner or need a quick refresher, this course will walk you through the core concepts that appear in many introductory quizzes. By the end of the lesson you will understand how Python assigns values, how its data types behave, and how to work with common structures such as lists, sets, dictionaries, and loops.
1. Assignment Operator in Python
One of the first things you learn when writing Python code is how to store a value in a variable. The correct operator for this purpose is the single equals sign (=). Unlike some other languages that use := or <-, Python’s assignment syntax is simple and intuitive.
- Correct syntax:
my_var = 10 - Common mistake: using
==(equality comparison) instead of=.
Remember: = assigns, == compares.
2. Division and the type() Function
Python distinguishes between integer division (//) and true division (/). When you divide two integers with the single slash, the result is a float. The built‑in type() function reveals the data type of any expression.
x = 10
result = x / 2
print(type(result)) # Output: <class 'float'>
Thus, evaluating type(x / 2) when x = 10 returns <class 'float'>. This behavior is essential when you need precise decimal results rather than integer truncation.
3. Valid Identifier Names
Identifiers are names you give to variables, functions, classes, and other objects. Python follows a clear set of rules:
- They must start with a letter (a‑z, A‑Z) or an underscore (
_). - Subsequent characters can be letters, digits, or underscores.
- They cannot be a reserved keyword such as
fororwhile.
Examples of valid identifiers:
my_variable_my_variable(leading underscore is allowed)variable2
Examples of invalid identifiers:
2nd_variable(starts with a digit)for(reserved keyword)
4. Boolean Logic: or Operator
Python’s logical operators evaluate expressions to True or False. The or operator returns True if at least one operand is true. Consider the expression:
print(True or False)
The output is True because the first operand is already true, making the whole expression true regardless of the second operand.
5. Determining Length with len()
The built‑in len() function is the go‑to tool for counting elements in sequences such as lists, tuples, strings, and even dictionaries (where it counts keys). It replaces language‑specific functions like count(), size(), or length() that you might see in other programming environments.
my_list = [1, 2, 3]
print(len(my_list)) # Output: 3
Using len() consistently improves code readability and aligns with Pythonic conventions.
6. Working with Sets
Sets are unordered collections of unique elements. Adding an element that already exists does not change the set, because duplicates are automatically ignored.
my_set = {1, 2, 3}
my_set.add(2)
print(my_set) # Output: {1, 2, 3}
Notice that the output does not contain a duplicate 2. This property makes sets ideal for membership testing and eliminating duplicate data.
7. Dictionary Access and KeyError
Dictionaries map keys to values. When you try to retrieve a value using a key that does not exist, Python raises a KeyError. This exception signals that the requested key is missing from the dictionary.
my_dict = {"a": 1, "b": 2}
print(my_dict["c"]) # Raises KeyError
If you need a safe lookup, use the get() method, which returns None (or a default you provide) instead of raising an error:
value = my_dict.get("c") # Returns None
8. The range() Function and Loops
The range() function generates a sequence of numbers. When used in a for loop, it iterates from the start value (default 0) up to, but not including, the stop value.
for i in range(3):
print(i)
# Output:
# 0
# 1
# 2
Understanding range() is crucial for controlling loop execution and creating index‑based operations.
9. Summary of Key Concepts
- Use
=for assignment;==is for comparison. - Division with
/yields afloat; check types withtype(). - Valid identifiers start with a letter or underscore and cannot be keywords.
- The
oroperator returnsTrueif any operand is true. - Count elements with
len(). - Sets store unique items; adding duplicates has no effect.
- Accessing a missing dictionary key raises
KeyError; useget()for safe access. range()produces sequential numbers for loops, starting at 0 by default.
10. Frequently Asked Questions (FAQ)
What is the difference between = and :=?
Python 3.8 introduced the walrus operator (:=) for assignment expressions inside other statements, but the classic = remains the primary assignment operator.
Can I use len() on a set?
Yes. len() works on any collection that implements the __len__ method, including sets, dictionaries, lists, and tuples.
Why does print(True or False) not print "True or False"?
Because or evaluates the boolean expression and returns the resulting boolean value, not the literal string.
