Chapter 6: Python Fundamentals
📌 Overview
This chapter introduces the basic building blocks of Python programming. It explains how
Python programs are written, executed, and how various programming constructs like
variables, data types, operators, and expressions work.
1. Introduction to Python
● Python is a high-level, interpreted, object-oriented programming language.
● Easy to learn due to simple syntax similar to English.
● Extensively used in:
● Web development
● Data science
● Artificial intelligence
● Scientific computing
2. Character Set
Python uses:
● Letters (A–Z, a–z)
● Digits (0–9)
● Special symbols (+, -, *, %, @, &, etc.)
● White space characters (space, tab, newline)
● Other characters (punctuation, brackets, quotes, etc.)
3. Tokens in Python
● Token = smallest unit in a Python program.
● Types:
1. Keywords – Reserved words with special meaning (e.g., if, else, while, class, import).
Cannot be used as variable names.
[Link] – Names given to variables, functions, classes.
Rules:
● Can contain letters, digits, underscore (_).
● Cannot start with a digit.
● Case-sensitive.
● No keywords allowed.
● Examples: num1, Student_Name.
[Link] – Data values used in the program.
[Link] Literals → Integers, Floats, Complex numbers
1. String Literals → "Hello", 'Python'
[Link] Literals → True, False
[Link] Literal → None
[Link] – Symbols used to perform operations.
[Link] – Characters that structure code (:, ,, (), [], {}).
4. Variables in Python
A variable is a name given to a memory location.
Features:
● No explicit declaration required.
● Type is decided dynamically (Dynamic typing).
● Example:
x = 10
name = "Aaryahi"
pi = 3.14
5. Data Types in Python
Python has the following standard data types:
Numeric Types:
● int → integers (e.g., 5, -3, 1000)
● float → decimal numbers (e.g., 3.14, -2.5)
● complex → numbers with real & imaginary parts (e.g., 2 + 3j)
Boolean Type: True / False
● String Type: Sequence of characters ("Hello", 'Python')
● None Type: Represents null or no value.
6. Operators in Python
Python supports many operators:
(a) Arithmetic Operators
+ - * / % ** //
** → Exponentiation
// → Floor division
(b) Relational Operators
== != > < >= <=
(c) Logical Operators
and or not
(d) Assignment Operators
= += -= = /= %= *= //=
(e) Membership Operators
in, not in
Check presence of element in a sequence.
(f) Identity Operators
is, is not
Check if two objects share the same memory location.
(g) Bitwise Operators
& | ^ ~ << >>
7. Expressions
An expression is a combination of variables, constants, and operators that produces a value.
Example:
result = (a + b) * c
8. Input and Output in Python
Input → input() function
name = input("Enter your name: ")
Output → print() function
print("Hello", name)
Formatted Output:
print(f"My age is {age}")
print("The sum is:", a+b)
9. Type Conversion in Python
Implicit Type Conversion (Type Casting): Done automatically by Python.
x = 10
y = 2.5
z = x + y # z becomes float
Explicit Type Conversion: Done manually using functions → int(), float(), str(), bool().
num = int("123") # String converted to integer
10. Python Statements
Simple Statement: Single executable statement.
print("Hello")
Compound Statement: Group of statements with control structures (if, for, while).
11. Comments in Python
Used to make code more readable.
● Single-line comment: #
● Multi-line comment: Triple quotes (''' ''' or """ """)
12. Errors in Python
Syntax Errors: Mistakes in code structure.
Runtime Errors: Errors during execution (e.g., divide by zero).
Logical Errors: Incorrect results due to wrong logic.