← Back to quizzesFree quiz

Fundamentals of Python Syntax and Data Types

Welcome to this comprehensive module on Python fundamentals. In this lesson you will master the core syntax rules, common operators, and essential data types that form the backbone of Python…

10 questions~5 min
Fundamentals of Python Syntax and Data Types — Qwi
0 / 10
Score: 0%
1

Which operator correctly assigns the value 5 to variable `a` in Python?

2

What is the result of `print(10 % 3)` in Python?

3

Which built‑in function returns the data type of a variable `x`?

4

Given `my_list = [10, 20, 30, 40]`, what does `print(my_list[-2])` output?

5

Which statement correctly describes the difference between `is` and `==` in Python?

6

What will be printed by the following code? ```python x = 2 ** 4 print(x) ```

7

Which of the following is a valid variable name in Python?

8

What does `type(10 / 2)` return in Python 3?

9

When iterating with `for i in range(3): print(i)`, which sequence is printed?

10

Which built‑in function creates an empty list in Python?

Fundamentals of Python Syntax and Data Types

Welcome to this comprehensive module on Python fundamentals. In this lesson you will master the core syntax rules, common operators, and essential data types that form the backbone of Python programming. By the end of the course you will be able to write correct assignment statements, understand the difference between identity and equality, and work confidently with lists, numbers, and built‑in functions.

1. Assignment Operators in Python

Python uses a single, straightforward assignment operator: the equals sign (=). Unlike some other languages that have multiple assignment symbols, Python’s = assigns the value on the right‑hand side to the variable on the left‑hand side.

  • Correct usage: a = 5
  • Common misconceptions:
    • a <- 5 – This syntax belongs to R, not Python.
    • a := 5 – The walrus operator (:=) is used for assignment expressions inside other statements, not for simple variable assignment.
    • a == 5 – This is a comparison operator that checks equality, not an assignment.

Understanding the assignment operator is crucial because it determines how data is stored and later accessed in your programs.

2. Modulo Operator (%) and Its Result

The modulo operator returns the remainder after division. It is frequently used for tasks such as checking even/odd numbers, cycling through indices, and creating periodic patterns.

print(10 % 3)  # Output: 1

Explanation: 10 divided by 3 equals 3 with a remainder of 1, so the expression evaluates to 1. This operator works with integers and floats, but the result type follows the type of the operands.

3. Determining the Data Type of a Variable

Python provides the built‑in function type() to retrieve the class of an object. This is especially useful for debugging and for writing generic code that adapts to different input types.

x = 42
print(type(x))  # Output: 

name = "Alice"
print(type(name))  # Output: 

Other functions such as print() display values, while typeof() and class() do not exist in Python’s standard library.

4. List Indexing and Negative Indices

Lists are ordered collections that support zero‑based indexing. Python also allows negative indices, which count from the end of the list.

my_list = [10, 20, 30, 40]
print(my_list[-2])  # Output: 30

Here -2 refers to the second‑last element, which is 30. Negative indexing is a powerful feature for accessing elements without needing to know the exact length of the list.

5. Identity (is) vs Equality (==)

Python distinguishes between object identity and value equality:

  • is checks whether two references point to the exact same object in memory.
  • == checks whether the values of two objects are equivalent, invoking the object's __eq__ method.

Example:

a = [1, 2, 3]
 b = a          # b references the same list object
 c = [1, 2, 3]  # c is a new list with the same values

print(a is b)   # True – same object
print(a == b)   # True – same values
print(a is c)   # False – different objects
print(a == c)   # True – values are equal

Choosing the correct operator prevents subtle bugs, especially when working with mutable objects.

6. Exponentiation Operator (**)

Python uses the double asterisk (**) for exponentiation. It raises the left operand to the power of the right operand.

x = 2 ** 4
print(x)  # Output: 16

The result is an integer when both operands are integers and the exponent is non‑negative. For fractional exponents, the result becomes a float.

7. Valid Variable Names

Variable naming rules in Python are simple yet strict:

  • Names must start with a letter (a‑z, A‑Z) or an underscore (_).
  • Subsequent characters can be letters, digits, or underscores.
  • Names cannot be Python reserved keywords (e.g., while, for).

Examples of valid names:

  • _my_variable
  • counter1
  • dataSet

Invalid examples include while (keyword), 2nd_variable (starts with a digit), and for (keyword).

8. Division Operators and Result Types in Python 3

Python 3 introduced true division (/) that always returns a float, even when the division is mathematically exact.

result = 10 / 2
print(type(result))  # Output: 

Contrast this with the floor division operator (//), which returns an integer when both operands are integers.

9. Quick Review Quiz

Test your understanding with the following questions. Review the explanations above to confirm each answer.

  1. Which operator correctly assigns the value 5 to variable a?
    • a = 5 (Correct)
    • a <- 5
    • a := 5
    • a == 5
  2. What is the result of print(10 % 3)?
    • 1 (Correct)
    • 0.333
    • 3.0
    • 3
  3. Which built‑in function returns the data type of a variable x?
    • type() (Correct)
    • print()
    • typeof()
    • class()
  4. Given my_list = [10, 20, 30, 40], what does print(my_list[-2]) output?
    • 30 (Correct)
    • 20
    • 40
    • IndexError
  5. Which statement correctly describes the difference between is and ==?
    • is checks identity, == checks equality (Correct)
    • Both check object identity
    • is checks equality, == checks identity
    • Both check value equality
  6. What will be printed by the code x = 2 ** 4; print(x)?
    • 16 (Correct)
    • 8
    • 10
    • Lỗi
  7. Which of the following is a valid variable name in Python?
    • _my_variable (Correct)
    • while
    • for
    • 2nd_variable
  8. What does type(10 / 2) return in Python 3?
    • (Correct)

10. Key Takeaways

  • Use = for assignment; == is for equality comparison.
  • The modulo operator (%) yields the remainder of division.
  • type() reveals an object's class, essential for dynamic typing.
  • Negative list indices count from the end, simplifying access to tail elements.
  • is checks identity, while == checks value equality.
  • Exponentiation uses **, and true division (/) always returns a float.
  • Variable names must start with a letter or underscore and cannot be keywords.

By mastering these fundamentals, you lay a solid foundation for more advanced Python topics such as functions, classes, and modules. Keep practicing, and refer back to this guide whenever you encounter syntax questions.