University Course • EduArtha

Python Programming

Master Python from scratch — variables, control flow, functions, OOP, file handling, and regular expressions. Includes 15 complete lab experiments with solutions.

📚 6 Units  |  14 Chapters  |  15 Lab Programs  |  Complete Solutions

Unit I

Setting Up Your Programming Environment

Python installation, variables, expressions & statements

Chapter 1

Setting Up & Hello World

Learning Objectives

  • Understand Python versions and choose the right one
  • Install Python on Windows and configure PATH
  • Write and run your first Python program
  • Use IDLE, VS Code, and the command line
  • Master the print() and input() functions

1.1 What is Python?

Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum in 1991. It emphasizes code readability with its clean syntax and indentation-based block structure. Python is used everywhere — web development (Django, Flask), data science (NumPy, Pandas), AI/ML (TensorFlow, PyTorch), automation, and more.

1.2 Python 2 vs Python 3

FeaturePython 2Python 3
Printprint "hello" (statement)print("hello") (function)
Integer Division5/2 = 25/2 = 2.5
UnicodeASCII by defaultUnicode by default
Inputraw_input()input()
StatusEnd of Life (Jan 2020)Use this!

Always Use Python 3

Python 2 reached End of Life on January 1, 2020. All new projects should use Python 3.x. As of 2025, the latest stable version is Python 3.12+.

1.3 Installing Python on Windows

Step-by-Step Installation

  1. Go to https://www.python.org/downloads/
  2. Click "Download Python 3.12.x" (latest stable)
  3. IMPORTANT: Check ✅ "Add Python to PATH" on the installer
  4. Click "Install Now" (default settings are fine)
  5. Verify: open Command Prompt, type python --version
Command Prompt
# Verify Python installation
C:\> python --version
Python 3.12.4

# Verify pip (package installer)
C:\> pip --version
pip 24.0 from C:\Python312\Lib\site-packages\pip (python 3.12)

1.4 Your First Python Program

Create a file called hello.py and type:

Python
# hello.py — Your first Python program!
print("Hello, World!")
Hello, World!

Run it from the terminal:

Command Prompt
C:\projects> python hello.py
Hello, World!

1.5 Ways to Run Python

MethodBest ForHow
IDLEQuick testing, beginnersComes with Python, search "IDLE" in Start
VS CodeReal projects, debuggingInstall Python extension, press F5 to run
Command LineScripts, automationpython filename.py
Interactive ModeQuick experimentsType python in terminal → type code
Jupyter NotebookData science, learningpip install jupyterjupyter notebook

1.6 The print() Function

Python
# Basic printing
print("Hello, World!")           # String
print(42)                         # Number
print(3.14)                       # Float
print(True)                       # Boolean

# Multiple values
print("Name:", "Alice", "Age:", 25)
# Output: Name: Alice Age: 25

# Custom separator and end
print("A", "B", "C", sep="-")     # Output: A-B-C
print("Hello", end=" ")             # No newline at end
print("World")                     # Output: Hello World

# Escape characters
print("Line1\nLine2")              # Newline
print("Tab\there")                 # Tab
print("She said \"hi\"")            # Escaped quotes

1.7 The input() Function

Python
# Get user input
name = input("What is your name? ")
print("Hello,", name)

# input() always returns a string!
age_str = input("Enter your age: ")    # Returns "25" (a string)
age = int(age_str)                       # Convert to integer
print("Next year you'll be", age + 1)

# Shortcut: convert inline
num = int(input("Enter a number: "))
print("Double:", num * 2)

Exercises

Exercise 1.1: Write a program that asks for the user's name and age, then prints a greeting
Python
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}! You are {age} years old.")
print(f"In 5 years, you'll be {age + 5}.")
Exercise 1.2: Write a program to calculate the area of a rectangle
Python
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
perimeter = 2 * (length + width)
print(f"Area = {area}")
print(f"Perimeter = {perimeter}")

Industry Application: Automated Server Health Check Script

At companies like AWS, Google Cloud, and Azure, DevOps engineers write Python scripts to perform automated server health checks. These scripts run periodically (via cron jobs) to verify system status, Python environment, and uptime — printing critical diagnostics to monitoring dashboards.

Python
import platform
import sys
import os
from datetime import datetime

# Automated Server Health Check Script
print("═" * 50)
print("🖥️  SERVER HEALTH CHECK REPORT")
print("═" * 50)
print(f"Python Version : {sys.version.split()[0]}")
print(f"OS             : {platform.system()} {platform.release()}")
print(f"Machine        : {platform.machine()}")
print(f"Hostname       : {platform.node()}")
print(f"Timestamp      : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"CPU Count      : {os.cpu_count()}")
print("═" * 50)
print("✅ Status: All systems operational")
══════════════════════════════════════════════════ 🖥️ SERVER HEALTH CHECK REPORT ══════════════════════════════════════════════════ Python Version : 3.12.4 OS : Windows 11 Machine : AMD64 Hostname : PROD-SERVER-01 Timestamp : 2025-06-15 14:32:07 CPU Count : 8 ══════════════════════════════════════════════════ ✅ Status: All systems operational

Quick Quiz — Chapter 1

Q1. Who created the Python programming language?

  1. James Gosling
  2. Guido van Rossum
  3. Dennis Ritchie
  4. Bjarne Stroustrup
✅ Answer: (b) Guido van Rossum — He created Python in 1991 at CWI, Netherlands.

Q2. What is the correct syntax for printing "Hello" in Python 3?

  1. echo "Hello"
  2. print("Hello")
  3. printf("Hello")
  4. console.log("Hello")
✅ Answer: (b) print("Hello") — print() is a built-in function in Python 3 that outputs to the console.

Q3. What data type does the input() function always return?

  1. int
  2. float
  3. str
  4. bool
✅ Answer: (c) str — input() always returns a string, even if the user types a number. Use int() or float() to convert.

Q4. Python is an _______ language, meaning code is executed line by line.

  1. compiled
  2. interpreted
  3. assembled
  4. machine-level
✅ Answer: (b) interpreted — Python uses an interpreter that executes code line by line, unlike compiled languages like C/C++.

Q5. What is the default file extension for Python scripts?

  1. .pt
  2. .py
  3. .python
  4. .pn
✅ Answer: (b) .py — Python source files use the .py extension (e.g., hello.py, app.py).

Chapter Summary

  • Python 3 is the current standard — always use Python 3.x
  • Always check "Add to PATH" when installing on Windows
  • print() outputs to the console, input() reads from the user
  • input() always returns a string — use int() or float() to convert
  • Use IDLE for quick tests, VS Code for real projects