ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum in 1991. It is designed with a strong emphasis on code readability, making it one of the easiest languages for beginners to learn while remaining powerful enough for professionals. Python is used everywhere today, from simple scripts to web applications, data analysis, artificial intelligence, and scientific computing.

The name Python comes from the British comedy show "Monty Python's Flying Circus", not from the snake. Python follows a philosophy called "batteries included", meaning it comes with a rich standard library of modules that handle common tasks such as file handling, mathematics, and networking without needing extra installation.

This chapter lays the foundation of programming in Python. We will explore how to install and run Python, the structure of a Python program, the rules for writing identifiers and keywords, and how data is stored using variables. We will also see how Python's interactive mode (IDLE) differs from script mode, and how comments and print statements help us understand and debug code. Every concept introduced here will be used constantly throughout the remaining Python chapters.

2. Getting Started with Python

Python can be downloaded free from python.org. After installation, the user gets two ways of working with Python:

Example of interactive mode:

>>> 5 + 3
8
>>> print("Hello World")
Hello World

A script file first.py would contain the same statements and is executed using the command python first.py.

3. Character Set and Tokens

The set of all characters that Python can recognise is called its character set. It includes letters (A-Z, a-z), digits (0-9), special symbols (such as +, -, , /, %, #, @), and whitespace (space, tab, newline). Python programs are broken down by the interpreter into small units called tokens*. The five types of tokens are:

Rules for Identifiers

  1. Must start with a letter (A-Z or a-z) or an underscore _.
  2. May be followed by letters, digits, or underscores.
  3. Cannot start with a digit.
  4. Cannot contain spaces or special symbols like @, $, %.
  5. Cannot be a keyword.
  6. Python is case-sensitive, so age and Age are different identifiers.

Valid identifiers: name, _count, roll_no, total2. Invalid identifiers: 2name (starts with digit), my name (contains space), class (keyword), na$me (special symbol).

4. Variables and Assignments

A variable is a name that refers to a storage location in memory that holds a value. In Python, unlike C or Java, you do not need to declare the type of a variable before assigning a value; the type is inferred automatically from the value assigned. This is called dynamic typing.

name = "Anita"
age = 17
marks = 95.5
is_pass = True

Here name holds a string, age holds an integer, marks holds a float, and is_pass holds a boolean. The assignment operator = stores the value on its right into the variable on its left. Multiple variables can be assigned in one line:

x, y = 10, 20

5. Data Types

Python has several built-in data types. The commonly used ones are:

The type() function returns the type of a value:

print(type(42))
print(type(3.14))
print(type("Hello"))

Output:

<class 'int'>
<class 'float'>
<class 'str'>

6. Operators and Expressions

Python provides a rich set of operators. The main categories are:

a = 17
b = 5
print(a // b)
print(a % b)
print(a ** 2)
print(a > b)

Output:

3
2
289
True

Here 17 // 5 gives 3 (floor division), 17 % 5 gives 2 (remainder), and 17 ** 2 gives 289.

7. Taking Input and Printing Output

The input() function reads a line of text from the keyboard. It always returns a string, so if we need a number, we must convert it using int() or float().

name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name)
print("You will be", age + 1, "next year")

The print() function displays output. By default it separates multiple values with a space and ends with a newline. The sep and end parameters can change this behaviour:

print("a", "b", "c", sep="-", end="!")

Output: a-b-c!

8. Type Conversion and Its Importance

When data moves between variables, functions, and expressions, Python sometimes needs to convert a value from one type to another. This conversion is called type conversion or type casting. It can happen automatically, as when an integer and a float are added together: the expression 5 + 2.5 produces the float 7.5, because Python promotes the integer to a float before performing the addition. This automatic behaviour is called implicit type conversion. However, Python never converts a string to a number implicitly, so writing "5" + 2 would raise a TypeError. When such a conversion is required, the programmer must do it explicitly using functions such as int(), float(), and str().

The input() function always returns a string, which is why so many beginners see unexpected errors when they try to do arithmetic directly on the value entered by the user. For example, if a student types 45 in response to a prompt, input() returns the string "45". Adding 5 to this string with the expression "45" + 5 fails, because Python cannot combine a string and an integer. The correct approach is to wrap the input in int(), as in age = int(input("Enter age: ")). The same logic applies to float() for decimal values. Understanding when conversion happens automatically and when it must be written explicitly removes a whole class of common programming errors, and it is a favourite exam topic.

9. Writing a Complete First Program

A good way to consolidate the basics is to write a small program that uses variables, input, arithmetic, and output together. Suppose we want to compute the area of a rectangle. We first read the length and breadth using input(), convert each to a float, multiply them, and finally print the result. The program shows how the three stages of input, processing, and output fit together in the order that Python executes them. Another classic program converts a temperature in Celsius to Fahrenheit using the formula F = (9/5) C + 32. In Python, care is needed with the fraction 9/5: since both 9 and 5 are integers, the division gives the float 1.8, which is exactly what the formula requires. Reading such small programs line by line, predicting the output mentally, and then running them to check the prediction is the most effective way to master the fundamentals of Python and to prepare for output-prediction questions in the examination.

Quick Revision Tables

Table 1: Python Tokens and Examples

Token Description Example
Keyword Reserved word if, while, def
Identifier Name given by user age, marks
Literal Constant value 25, 3.14, "Hi"
Operator Performs operation +, %, **
Punctuator Separates code ( ), ,, :

Table 2: Arithmetic Operators in Python

Operator Operation Example Result
+ Addition 7 + 2 9
- Subtraction 7 - 2 5
* Multiplication 7 * 2 14
/ Division 7 / 2 3.5
// Floor division 7 // 2 3
% Modulus 7 % 2 1
** Exponent 2 ** 3 8

Mind Map

graph TD A["Basis of Python"] --> B["Python Modes"] A --> C["Tokens"] A --> D["Variables and Data Types"] A --> E["Operators"] A --> F["Input and Output"] B --> B1["Interactive Mode"] B --> B2["Script Mode (.py)"] C --> C1["Keywords"] C --> C2["Identifiers"] C --> C3["Literals"] C --> C4["Operators"] C --> C5["Punctuators"] D --> D1["int, float, complex"] D --> D2["str, bool"] D --> D3["list, tuple, dict, set"] E --> E1["Arithmetic"] E --> E2["Relational"] E --> E3["Logical"] F --> F1["input()"] F --> F2["print()"]

Important Diagrams (SVG)

Diagram 1: Types of Python Tokens

Python Tokens Python Program Broken into tokens Keywords Reserved words if, while, def Cannot be renamed Identifiers User-defined names age, roll_no Case sensitive Literals Constant values 25, 3.14, "Hi" Fixed in program Operators Symbols that act + - * / % ** Perform operations Punctuators Grouping symbols ( ) [ ] , : Separate code Golden Rule An identifier must never be a keyword, must not start with a digit, and may not contain spaces

Diagram 2: Flow of a Python Program (Input-Process-Output)

Python Program Execution Flow Input input() function Processing Operators and expressions Variables store results Output print() function Example Program a = int(input("Enter a: ")) b = int(input("Enter b: ")) print("Sum is", a + b) Golden Rule input() always returns a string, so convert with int() or float() before arithmetic

Common Mistakes

  1. Using a keyword as a variable name, such as class = 10, which produces a syntax error.
  2. Starting an identifier with a digit, such as 2name.
  3. Forgetting that Python is case-sensitive, so Name and name are different variables.
  4. Using / when floor division // was intended, since / always returns a float.
  5. Adding a number to the result of input() without converting it with int(), which causes a type error.
  6. Mixing single and double quotes incorrectly, or forgetting to close a string.
  7. Assuming = means equality; in Python = assigns while == compares.
  8. Writing elif as else if in Python, which is a syntax error.

Exam Tips

  1. Practise identifier validity questions; they are almost guaranteed in the exam.
  2. Remember that // performs floor division and / always gives a float.
  3. Learn the exact behaviour of % for negative numbers: the result has the sign of the divisor in Python.
  4. Be ready to predict the output of short programs using print() with sep and end.
  5. Memorise the type() output format such as <class 'int'>.
  6. Always convert user input using int() or float() when numbers are needed.

Conclusion

The basis of Python lies in its clean syntax and the simple concepts of tokens, variables, data types, operators, and input-output. Understanding what makes an identifier valid, how data flows through a program, and how operators behave prepares the student for writing meaningful programs. Python's dynamic typing and readable structure make it an ideal first programming language, and the fundamentals covered here are the stepping stones for data handling, control structures, lists, dictionaries, and visualisation that follow in the later chapters.