Python Fundamentals is the chapter that turns a beginner into a programmer. After getting familiar with the Python environment and its basic tokens, the student now learns how data is stored in variables, how different kinds of literals are written, how operators combine values into expressions, and how the user is involved in a program through the input() function. These elements, the vocabulary and grammar of Python, are used in every program ever written in the language.
A Python program is a sequence of statements, and each statement is built from tokens. At the heart of programming lies the concept of a variable: a named location in memory that holds a value. In Python, variables are created simply by assigning a value to a name; there is no explicit declaration. The type of the variable is inferred from the value, and it can change later because Python is dynamically typed. This flexibility is powerful but demands care, because a variable's current type determines which operations are valid on it.
The chapter also introduces operators, which perform computations on operands. Python provides arithmetic, relational, logical, assignment, identity and membership operators, each with a well-defined precedence that decides the order of evaluation in an expression. Combined with the input() function, which makes programs interactive, these tools allow a student to write their first meaningful programs that take data from the user, process it, and produce results.
A variable is a name that refers to a value stored in the computer's memory. In Python, a variable is created the first time a value is assigned to it using the assignment operator =.
age = 16
name = "Aarav"
marks = 92.5
is_passed = True
Key properties of variables in Python:
x = 10
y = x # y holds 10
x = 20 # x now holds 20; y still holds 10
print(x, y) # prints 20 10
Literals are the data values that appear directly in a program. Python supports several kinds:
Whole numbers without a decimal point, such as 42, -7, 0. They can be written in decimal form. Python 3 also allows prefixes for other bases: 0b for binary, 0o for octal and 0x for hexadecimal.
a = 42
b = 0b1010 # binary for 10
c = 0o17 # octal for 15
d = 0x1F # hexadecimal for 31
Real numbers with a fractional part, such as 3.14, -0.001, 2.5e3. The e notation means "times 10 to the power of", so 2.5e3 is 2500.0.
Sequences of characters enclosed in single quotes, double quotes or triple quotes. Single and double quotes are equivalent; triple quotes allow multi-line strings.
city = 'Delhi'
school = "Central School"
message = """This is a
multi-line string."""
The values True and False, used for logical conditions. Note that True and False start with capital letters in Python.
The special value None represents the absence of a value, like a null in other languages.
Lists, tuples, dictionaries and sets are also literal forms, but they are covered in later chapters.
An operator is a symbol that performs an operation on one or more operands. Python groups its operators into several categories.
Used for mathematical calculations:
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 7 + 3 = 10 |
| - | Subtraction | 7 - 3 = 4 |
| * | Multiplication | 7 * 3 = 21 |
| / | True division (float) | 7 / 2 = 3.5 |
| // | Floor division (integer) | 7 // 2 = 3 |
| % | Modulus (remainder) | 7 % 2 = 1 |
| ** | Exponentiation | 2 ** 3 = 8 |
Compare two values and return True or False: == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal), <= (less than or equal).
print(5 == 5) # True
print(5 != 3) # True
print(5 > 10) # False
print(5 <= 5) # True
Combine boolean values: and, or, not. They work exactly as in Boolean logic from an earlier chapter.
print(True and False) # False
print(True or False) # True
print(not True) # False
Besides the basic = operator, Python offers compound assignment operators that combine assignment with an operation: +=, -=, =, /=, //=, %=, *=. For example, x += 5 is equivalent to x = x + 5.
count = 10
count += 5 # count becomes 15
count -= 3 # count becomes 12
count *= 2 # count becomes 24
The operators is and is not check whether two variables refer to the same object in memory, rather than whether their values are equal.
The operators in and not in check whether a value is present inside a sequence such as a string, list or tuple.
print("a" in "python") # False
print("h" in "python") # True
An expression is a combination of operands and operators that evaluates to a single value. For example, (a + b) * c / 2 is an expression. When an expression contains many operators, Python follows a fixed order called operator precedence, and within the same precedence level, associativity (usually left to right).
The precedence of Python operators from highest to lowest (a partial list):
result = 2 + 3 * 4 ** 2 # 4 ** 2 = 16; 3 * 16 = 48; 2 + 48 = 50
print(result) # 50
To avoid confusion, parentheses should be used to make the intended order explicit.
The built-in input() function reads a line of text from the keyboard. In Python 3, input() always returns a string, even if the user types a number. Therefore, numeric input must be converted using int() or float().
name = input("Enter your name: ")
age = int(input("Enter your age: "))
height = float(input("Enter your height in cm: "))
print("Hello", name)
print("Age next year:", age + 1)
If the user enters 16 for age, input() returns the string "16", and int("16") converts it to the integer 16, allowing arithmetic. Forgetting this conversion is one of the most common beginner errors: it produces a TypeError when trying to add a string and an integer.
Python has several built-in data types. The most important for now are:
The type of a value can be checked with the built-in function type().
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("Hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
Type conversion functions int(), float(), str() and bool() convert a value from one type to another. For example, float(5) gives 5.0, int("12") gives 12, and str(99) gives "99".
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Division:", a / b)
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
area = length * breadth
perimeter = 2 * (length + breadth)
print("Area:", area)
print("Perimeter:", perimeter)
| Operator | Operation | 7 and 3 | 7 and 2 |
|---|---|---|---|
| / | True division | 2.333... | 3.5 |
| // | Floor division | 2 | 3 |
| % | Modulus | 1 | 1 |
| ** | Exponentiation | 343 | 49 |
| Level | Operators |
|---|---|
| 1 | ( ) parentheses |
| 2 | ** |
| 3 | +x, -x, not |
| 4 | *, /, //, % |
| 5 | +, - |
| 6 | <, >, <=, >=, ==, != |
| 7 | and |
| 8 | or |
| 9 | = assignment |
Python Fundamentals equips the student with the essential building blocks of every program: variables, literals, operators, expressions and user input. Variables hold values dynamically without declarations; literals give those values their form; operators, governed by a clear precedence, build expressions; and input() makes programs interactive, provided the returned string is converted to a number when needed. These fundamentals are exercised in every subsequent chapter. With data now stored in variables and combined by operators, the next chapter, Data Handling, explores the full range of Python data types, their mutability and the standard library modules that make Python so powerful.