TN Online TestSamacheer Kalvi practice

Python -Variables and Operators - Study Notes

Share this chapter: Telegram

Chapter Summary

Python is a general-purpose, interpreted, high-level programming language created by Guido Van Rossum at CWI in 1991. Known for its easy-to-read syntax, Python supports both procedural and object-oriented programming paradigms. This chapter covers the fundamental tools of Python, focusing on its Integrated Development and Learning Environment (IDLE). Python scripts can be executed in two modes: Interactive mode (using the interactive terminal prompt for immediate line-by-line feedback) and Script mode (saving commands as reusable files with a .py extension). It details basic input and output statements, the significance of indentation, code commenting, and lexical tokens like identifiers, keywords, operators, delimiters, and literals.

Learning Objectives

Key Concepts and Definitions

Interactive Mode

In this mode, Python code is typed directly at the interpreter command prompt (indicated by the >>> prompt). The interpreter processes each statement instantly and displays the outcome immediately on the screen.

Script Mode

In this mode, programmers write a complete script of Python statements in a text editor, save it as a file with the .py extension, and execute the entire program. Script mode is ideal for building reusable, editable, and modular source files.

Tokens

Tokens are the most fundamental, elementary lexical components that form a program's structure. Python recognizes five distinct token types: Identifiers, Keywords, Operators, Delimiters, and Literals.

Identifiers

An identifier is a user-defined name given to program elements like variables, functions, classes, or modules. Identifiers must begin with an alphabet letter or an underscore, followed by letters or digits, and cannot match any reserved keyword.

Keywords

Keywords are special reserved words with predefined meanings meant specifically for the Python interpreter to understand program structure. Examples include False, class, return, if, and while.

Literals

A literal is a raw, fixed data value assigned to a constant or variable in code. Python supports numeric literals (integers, floats, complex numbers), string literals, and Boolean literals (True or False).

Worked Methods

Dynamic Typing and Assignment

In Python, variables do not require explicit type declaration before use. A variable is created automatically when a value is assigned to it using the assignment operator (=). For example, writing x = 100 creates an integer variable. Python also supports multiple assignments in a single statement, such as a, b = 5, 10, which assigns 5 to a and 10 to b simultaneously.

User Input Processing

The input() function is utilized to collect data from the keyboard at runtime. It accepts all inputs as string data. To perform mathematical calculations, these strings must be explicitly cast to numeric types using type-conversion functions such as int() or float(). A standard example of this method is: num = int(input("Enter number: ")).

Ternary Operator Execution

The conditional (ternary) operator evaluates expressions in a single compact line, replacing standard multi-line if-else blocks. The basic syntax is: Variable_Name = [on_true] if [Test_Expression] else [on_false]. This is executed by evaluating the Test_Expression first; if true, the on_true value is assigned, otherwise the on_false value is assigned.

Common Exam Traps

The String Input Trap

A common error is attempting to add values directly obtained from the input() function without type casting. Since input() returns strings, the plus (+) operator performs string concatenation rather than mathematical addition. For example, if a user enters 5 and 6, x + y evaluates to "56" instead of 11.

Indentation Mismatches

Unlike languages that use curly braces, Python relies strictly on whitespace to group statements into blocks. Mixing tabs and spaces, or using inconsistent spacing within the same block, results in an IndentationError during execution.

Division Types Confusion

Students often confuse the standard division operator (/) with the floor division operator (//). Standard division always returns a decimal floating-point number (e.g., 10 / 5 returns 2.0). Floor division discards the fractional part and returns the quotient as an integer value (e.g., 10 // 3 returns 3).

Bitwise XOR vs. Exponentiation

Mistaking the caret symbol (^) for exponentiation is a frequent trap. In Python, the exponentiation operator is represented by double asterisks (**). Using 2 ^ 3 evaluates to 1 because ^ is the bitwise XOR operator, whereas 2 ** 3 correctly calculates 8.

Exam Tips List

Solved MCQs → Practice test →

More for this chapter

Book Back Questions10 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

More chapters in Computer Science

View all
1 Function 2 Data Abstraction 3 Scoping 4 Algorithmic Strategies 6 Control Structures 7 Python functions 8 Strings and String manipulation 9 Lists, Tuples, Sets and Dictionary 10 Python Classes and objects 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV files 14 Importing C++ programs in Python. 15 Data manipulation through SQL 16 Data visualization using pyplot: line chart, pie chart and bar chart