Python Fundamentals

Introduction to Python

1. Introduction to Python

Python is a high-level, interpreted, and general-purpose programming language. It was created by Guido van Rossum and first released in 1991. Its design philosophy emphasizes code readability, making it an ideal first language for students.

2. Key Features of Python

Python stands out because of its simplicity and power. Here are its core features:

  • Easy to Learn and Read: Python uses a very clear syntax that looks a lot like English. It uses indentation to define blocks of code instead of curly braces.
  • Interpreted Language: Python code is executed line-by-line by an interpreter. This makes debugging (finding errors) much easier.
  • Platform Independent (Portable): You can run the same Python code on Windows, macOS, or Linux without making changes.
  • Case Sensitive: In Python, Variable and variable are treated as two different things.
  • Large Standard Library: It comes with a vast collection of pre-written code (modules) that you can use to perform complex tasks easily.
  • Free and Open Source: You can download Python for free from python.org.

3. Executing a “Hello World” Program

The Code:

print("Hello World")

How it works:

  • print() is a built-in function used to display output.
  • The text inside the quotes is called a string.

4. Python Execution Modes

There are two primary ways to run your Python code. Understanding the difference is crucial for efficient coding.

A. Interactive Mode

In this mode, you type one line of code at a time at the Python prompt (>>>), and the interpreter executes it immediately after you press Enter.

  • Best for: Testing small snippets of code or doing quick calculations.
  • Limitation: The code is not saved. Once you close the window, your work is gone.

B. Script Mode

In script mode, you write your entire program in a text file and save it with a .py extension (e.g., myscript.py). You then run the entire file at once.

  • Best for: Writing long programs and projects that you want to save and run later.
  • How to use:
    1. Open a script editor (like IDLE, VS Code, or Notepad).
    2. Write your code.
    3. Save the file as filename.py.
    4. Run the file (usually by pressing F5 in IDLE).
FeatureInteractive ModeScript Mode
ExecutionLine-by-line, immediateAll at once
SavingCannot save codeCode is saved in a .py file
EditingHard to edit previous linesEasy to edit and re-run
UsageQuick testingBuilding applications

==================================================================


1. Python Character Set

A character set is a set of valid characters that can be used in a programming language.

Python supports the following characters:

(i) Letters

  • Uppercase letters: A to Z
  • Lowercase letters: a to z

Example:

name = "Rahul"

(ii) Digits

  • 0 to 9

Example:

age = 16

(iii) Special Symbols

Symbols used in Python programs.

Examples:

+  -  *  /  %  =  <  >  ( ) { } [ ] ; : " ' #

(iv) White Spaces

Spaces, tabs, and new lines are called white spaces.

Example:

x = 10
y = 20

2. Python Tokens

Tokens are the smallest individual units in a Python program.

Types of Python Tokens:

  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators
  5. Punctuators

2.1 Keywords

Keywords are reserved words that have special meaning in Python.

Examples:

if, else, while, for, break, continue, True, False, class

Example Program:

if 10 > 5:
    print("10 is greater")

Important Points

  • Keywords cannot be used as variable names.
  • Python keywords are written in lowercase except True, False, and None.

2.2 Identifiers

Identifiers are names given to different part of the program like:

  • Variables
  • Functions
  • Classes
  • Objects

Example:

marks = 95
student_name = "Aman"

Rules for Naming Identifiers

  1. Must start with a letter or underscore _
  2. Cannot start with a digit
  3. Cannot contain spaces
  4. Cannot use keywords
  5. Case-sensitive

Valid identifiers :

name
_marks
student1

Invalid identifiers:

1name
class
student name

2.3 Literals

Literals are fixed values used in a program.

Types of Literals

(i) Numeric Literals

Numeric literals are fixed numeric values written directly in a Python program. They represent numbers such as integers, decimals, or complex numbers.

Types of Numeric Literals in Python

1. Integer Literals (int)

Integer literals are whole numbers without decimal point.

They can be:

  • Positive
  • Negative
  • Zero

Examples:

10
-25
0

Program:

x = 100
print(x)

Output:

100

2. Floating Point Literals (float)

Numbers containing decimal point are called floating point literals.

Examples:

10.5
-3.14
0.0

Program:

pi = 3.14
distance = 1.2e4  # 1.2 * 10^4 = 12000.0
print(pi)           #3.14
print(distance)     #12000.0

Output:

3.14

3. Complex Literals (complex)

Complex numbers contain:

  • Real part
  • Imaginary part

Imaginary part is written using j.

Format:

a + bj

Examples:

2 + 3j
5j

Program:

x = 2 + 3j
print(x)    #(2+3j)

Output:

(2+3j)

Different Number Systems in Integer Literals

Python allows integers in different number systems.

TypePrefixExample
DecimalNo prefix25
Binary0b0b1010
Octal0o0o17
Hexadecimal0x0x1A

(i) Decimal Literal

Base 10 numbers.

Example:

x = 25
print(x)

(ii) Binary Literal

Base 2 numbers using digits 0 and 1.

Prefix: 0b

Example:

x = 0b1010
print(x)

Output:

10

(iii) Octal Literal

Base 8 numbers using digits 0 to 7.

Prefix: 0o

Example:

x = 0o17
print(x)

Output:

15

(iv) Hexadecimal Literal

Base 16 numbers using digits 0–9 and letters A–F.

Prefix: 0x

Example:

x = 0x1A
print(x)

Output:

26

Important Points

  • Numeric literals are fixed numbers in a program.
  • Python supports:
    • Integer literals
    • Floating point literals
    • Complex literals
  • Integer literals can also be written in binary, octal, and hexadecimal forms.

Quick Revision

LiteralExample
Integer25
Float3.14
Complex2+3j
Binary0b1010
Octal0o17
Hexadecimal0x1A

(ii) String Literals

"Hello"
'Python'

(iii) Boolean Literals

True
False

(iv) Special Literal

None

2.4 Operators

Operators are symbols used to perform operations.

Example:

a + b

2.5 Punctuators

Punctuators are symbols used to organize Python statements.

Examples:

( ) [ ] { } , : ; " '

Example:

print("Hello")

3. Variables

A variable is a named memory location used to store data.

Example:

x = 10
name = "Riya"

Here:

  • x stores integer value 10
  • name stores string value “Riya”

4. Concept of l-value and r-value

l-value

The variable present on the left side of assignment operator = is called l-value.

r-value

The value or expression present on the right side is called r-value.

Example:

x = 20

Here:

  • x → l-value
  • 20 → r-value

Another Example:

a = b + c
  • a → l-value
  • b + c → r-value

5. Comments in Python

Comments are notes written in a program to explain the code.

Python ignores comments during execution.


Types of Comments

(i) Single-line Comment

Starts with #

Example:

# This is a comment
print("Hello")

(ii) Multi-line Comment

Written using triple quotes.

Example:

"""
This is
multi-line comment
"""

6. Uses of Comments

Comments are used to:

  • Explain code
  • Improve readability
  • Make debugging easier
  • Help other programmers understand the program

Example:

# Calculating area of circle
radius = 7
area = 3.14 * radius * radius

7. print() Function

The print() function is used to display output on the screen.

Syntax

print(object(s), sep=separator, end=end)

Parameter Values

ParameterDescription
object(s)Any object, and as many as you like. Will be converted to string before printed
sep=’separatorOptional. Specify how to separate the objects, if there is more than one. Default is ‘ ‘
end=’endOptional. Specify what to print at the end. Default is ‘\n’ (line feed)
print("apple")
print("apple", "banana", "cherry")
print("apple", "banana", "cherry", sep="---")

Output:

apple
apple banana cherry
apple---banana---cherry

name = "Aman"
age = 16
print(name, age)

Output:

Aman 16


Using End (end)

Example:

print("Hello", end=" ")
print("World")

Output:

Hello World

8. Dynamic Typing

Python is a dynamically typed language.

This means:

  • No need to declare data type of variables.
  • Python automatically identifies the type.

Example:

x = 10
print(type(x))

x = "Hello"
print(type(x))

Output:

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

The same variable x stores different data types at different times.


9. Blocks and Indentation

Python uses indentation (spaces/tabs) to define blocks of code.

Indentation is very important in Python.

Example:

if 10 > 5:
    print("True")
    print("Inside if block")

print("Outside block")

Important Points

  • Usually 4 spaces are used for indentation.
  • Incorrect indentation gives error.

Wrong Example:

if 10 > 5:
print("Hello")

This causes:

IndentationError

10. Operators in Python

Operators perform operations on variables and values.


10.1 Arithmetic Operators

OperatorMeaning
+Addition
Subtraction
*Multiplication
/Division
%Modulus
//Floor Division
**Exponent

Example:

a = 10
b = 3

print(a + b)
print(a % b)
print(a ** b)

10.2 Relational Operators

Used for comparison.

OperatorMeaning
==Equal
!=Not Equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Example:

print(10 > 5)

Output:

True

10.3 Logical Operators

OperatorMeaning
andLogical AND
orLogical OR
notLogical NOT

Example:

x = True
y = False

print(x and y)
print(x or y)

10.4 Assignment Operators

OperatorExample
=x = 5
+=x += 2
-=x -= 2
*=x *= 2

Example:

x = 5
x += 3
print(x)

Output:

8

10.5 Membership Operators

OperatorMeaning
inPresent
not inNot Present

Example:

print("a" in "apple")

Output:

True

10.6 Identity Operators

OperatorMeaning
isSame object
is notDifferent object

Example:

x = 5
y = 5

print(x is y)

11. Expressions

An expression is a combination of Variables, Constants, Operators that produces a value.

Examples:

a + b
x * 5
10 > 3

Example Program:

a = 10
b = 20

c = a + b
print(c)

Output:

30


Summary

  • Python uses letters, digits, symbols, and spaces as character set.
  • Tokens are smallest units of Python program.
  • Variables store values.
  • Python supports dynamic typing.
  • Indentation defines code blocks.
  • Operators perform operations.
  • Expressions produce results.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top