ЁЯФм
ЁЯзм
ЁЯФн
ЁЯкР
ЁЯзк
тЖР Back to Dashboard
Font Size:

1. Introduction

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.

2. Variables and Assignment

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

3. Literals

Literals are the data values that appear directly in a program. Python supports several kinds:

3.1 Integer Literals

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

3.2 Floating-Point Literals

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.

3.3 String Literals

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."""

3.4 Boolean Literals

The values True and False, used for logical conditions. Note that True and False start with capital letters in Python.

3.5 None Literal

The special value None represents the absence of a value, like a null in other languages.

3.6 Special Literals for Collections

Lists, tuples, dictionaries and sets are also literal forms, but they are covered in later chapters.

4. Operators in Python

An operator is a symbol that performs an operation on one or more operands. Python groups its operators into several categories.

4.1 Arithmetic Operators

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

4.2 Relational (Comparison) Operators

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

4.3 Logical Operators

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

4.4 Assignment Operators

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

4.5 Identity Operators

The operators is and is not check whether two variables refer to the same object in memory, rather than whether their values are equal.

4.6 Membership Operators

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

5. Expression and Precedence of Operators

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):

  1. Parentheses ( )
  2. Exponentiation **
  3. Unary operators +x, -x, not
  4. Multiplication *, /, //, %
  5. Addition +, -
  6. Relational operators <, >, <=, >=, ==, !=
  7. Logical and
  8. Logical or
  9. Assignment =
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.

6. Taking Input from the User

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.

7. Working with Data Types

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".

8. Sample Programs

8.1 Simple Calculator Program

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)

8.2 Area of a Rectangle

length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
area = length * breadth
perimeter = 2 * (length + breadth)
print("Area:", area)
print("Perimeter:", perimeter)

Quick Revision Tables

Table 1: Arithmetic Operators

Operator Operation 7 and 3 7 and 2
/ True division 2.333... 3.5
// Floor division 2 3
% Modulus 1 1
** Exponentiation 343 49

Table 2: Operator Precedence (High to Low)

Level Operators
1 ( ) parentheses
2 **
3 +x, -x, not
4 *, /, //, %
5 +, -
6 <, >, <=, >=, ==, !=
7 and
8 or
9 = assignment

Mind Map

flowchart TD A[Python Fundamentals] --> B[Variables] B --> B1[Dynamic typing] B --> B2[Assignment =] A --> C[Literals] C --> C1[int] C --> C2[float] C --> C3[str] C --> C4[bool] C --> C5[None] A --> D[Operators] D --> D1[Arithmetic] D --> D2[Relational] D --> D3[Logical] D --> D4[Assignment] D --> D5[Identity] D --> D6[Membership] A --> E[Expressions] E --> E1[Precedence] E --> E2[Associativity] A --> F[User Input] F --> F1[input returns str] F --> F2[int and float conversion]

Important Diagrams (SVG)

Diagram 1: Precedence Pyramid

Operator Precedence in Python ( ) Parentheses * Exponentiation / // % + - = Assignment (lowest) Golden Rule: Higher precedence is evaluated first; use parentheses to override the default order.

Diagram 2: Taking and Converting User Input

Input and Type Conversion input() returns a STRING User types 16 but it becomes "16" int("16") gives 16 float("3.5") gives 3.5 Now arithmetic works on numbers age + 1 = 17 Golden Rule: input() always returns a string; convert numbers with int() or float() before doing arithmetic.

Common Mistakes

  1. Forgetting to convert input(): Trying to compute age + 1 when age is the string "16" raises a TypeError. Convert with int() first.
  2. Confusing = with ==: = is assignment, == is equality comparison. Writing if x = 5 causes a SyntaxError.
  3. Using / when // is intended: 7 / 2 is 3.5, but integer division 7 // 2 is 3. Choose based on the required result.
  4. Wrong precedence assumptions: 2 + 3 * 4 is 14, not 20, because * binds tighter than +. Use parentheses to be safe.
  5. Thinking True/False are 1/0 strings: Boolean literals are the capitalised keywords True and False, not "True" as a string.
  6. Swapping the operands of %: 7 % 2 is 1, not 3; % gives the remainder, while // gives the quotient.
  7. Reusing a variable name carelessly: Since Python is dynamically typed, changing a variable from int to str later can break type-sensitive operations.
  8. Using single = in comparisons inside conditions: A common source of logic errors in if statements.

Exam Tips

  1. Memorise the result of 7 // 2 = 3 and 7 % 2 = 1 because floor division and modulus are the most tested operators.
  2. Practise precedence questions such as 2 + 3 * 2 ** 2 and always show the evaluation order in answers.
  3. Know that input() returns a string in Python 3 and that int()/float() perform conversion.
  4. List all arithmetic operators with one example each; long-answer questions frequently ask for this table.
  5. Remember the compound assignment meaning: x += 5 is exactly x = x + 5.
  6. Be able to identify the type() output like for objective questions.
  7. Use the type conversion functions int(), float(), str(), bool() correctly in program-writing questions.

Conclusion

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.