Introduction to Python
Basics & Structure for BS 7th
Semester
What is Python?
• • High-level, interpreted programming
language
• • Easy to read and write (simple syntax)
• • Open-source and cross-platform
• • Widely used in web development, data
science, AI, and automation
• • Developed by Guido van Rossum, first
released in 1991
Why Learn Python?
• • Beginner-friendly and versatile
• • Large community and libraries
• • Supports multiple programming paradigms
• • Strong industry demand
• • Excellent for prototyping and production
Python Basic Structure
• • Comments: # This is a comment
• • Variables: x = 10
• • Input/Output: input(), print()
• • Indentation defines code blocks
• • Functions: def my_function():
• • Control statements: if, else, while, for
Python Example Code
• Example:
• name = input("Enter your name: ")
• print("Hello,", name)
• for i in range(5):
• print("Number:", i)
Data Types in Python
• • int → 10
• • float → 10.5
• • str → 'Hello'
• • bool → True/False
• • list → [1, 2, 3]
• • tuple → (1, 2, 3)
• • dict → {'key': 'value'}
• • set → {1, 2, 3}
Summary
• • Python is simple, powerful, and versatile
• • Basic structure includes variables, functions,
control flow
• • Indentation is crucial in Python
• • Supports multiple data types
• • Widely used in many fields (web, AI, data
science)
Practice Code: Variables & I/O
• Example 1:
• x=5
• y = 10
• print("Sum:", x + y)
• Example 2:
• name = input("Enter your name: ")
• print("Welcome,", name)
Practice Code: Control Flow
• Example 1: If-Else
• age = int(input("Enter your age: "))
• if age >= 18:
• print("You are an adult")
• else:
• print("You are underage")
• Example 2: Loop
• for i in range(5):
Flowchart: Python Execution Flow
• Start → Write Python Code → Save as .py →
Run Interpreter → Interpreter Reads Line by
Line → Executes → Output → End
Structure of a Python Program
• General Structure:
• 1. Comments & Documentation
• 2. Import Libraries
• 3. Define Variables & Constants
• 4. Define Functions & Classes
• 5. Main Program (control flow, loops,
conditions)
• 6. Output / Return Results
Practice Code: Functions
• def greet(name):
• return "Hello, " + name
• user = input("Enter your name: ")
• print(greet(user))
Mini Exercise
• Write a Python program that:
• • Takes two numbers as input
• • Prints their sum, difference, product, and
division
• • Checks if the first number is greater than the
second
• • Uses a function for at least one operation