Python Programming Basics: Variables, Data Types, and Your First Script

Python Programming Basics: Variables, Data Types, and Your First Script
1. Introduction
In our previous guide, we explored how algorithms and flowcharts help design step-by-step logic. The next natural step is translating that logic into a real programming language that a computer can execute.
Among the hundreds of programming languages used worldwide, Python is one of the most popular and student-friendly. Created by Guido van Rossum and released in 1991, Python emphasizes readability and simplicity. Its syntax looks very close to standard English, making it the ideal starting language for school students and ICT labs.
From building websites and automating routine tasks to artificial intelligence and data analysis, Python powers modern technology across the globe.
2. Why Learn Python?
 * Simple and Readable Syntax: Programs require fewer lines of code compared to older languages like C++ or Java.
 * Interpreted Language: Python runs line by line using an interpreter, which makes finding and correcting mistakes (debugging) immediate and straightforward.
 * Cross-Platform: The same Python script runs smoothly on Windows, Linux, and macOS without modifications.
 * Batteries Included: Python comes packed with a large standard library for math, file handling, web requests, and automation.
3. Core Concepts: Variables and Fundamental Data Types
What is a Variable?
Think of a variable as a labeled storage box in the computer’s memory. You give it a name, store a piece of information inside, and retrieve or modify that value whenever needed.
In Python, you do not need to declare variable types explicitly; Python automatically detects the type when you assign a value using the assignment operator (=):
school_name = "Utkramit Madhyamik Vidyalaya"
student_count = 250
is_lab_open = True

Core Primitive Data Types
| Data Type | Keyword | What It Stores | Example |
|---|---|---|---|
| Integer | int | Whole positive or negative numbers (without decimals) | roll_no = 15, temperature = -4 |
| Floating-Point | float | Real numbers containing decimal points | percentage = 88.5, pi = 3.14 |
| String | str | Textual data enclosed in single (') or double (") quotes | subject = "Computer Science" |
| Boolean | bool | Logical truth values (True or False) | is_active = True, has_passed = False |
4. Hands-on Lab Exercise: Writing and Running Your First Python Script
Students can write and execute their first Python script directly in their computer lab using Python IDLE or the command terminal.
Step 1: Open the Python Environment
 * On Windows, search for IDLE (Python) in the Start menu.
 * On Linux, open the terminal (Ctrl + Alt + T) and type python3 to start the interactive shell.
Step 2: Write the Student Record Script
Open a new file (File > New File) and type the following script:
# ==========================================
# ICT Lab Exercise: Student Record & Calculator
# ==========================================

# 1. Taking User Inputs
student_name = input("Enter student's name: ")
roll_number = int(input("Enter roll number: "))
theory_marks = float(input("Enter theory marks (out of 50): "))
practical_marks = float(input("Enter practical marks (out of 50): "))

# 2. Processing Data (Arithmetic Operations)
total_marks = theory_marks + practical_marks
percentage = (total_marks / 100) * 100

# 3. Outputting Formatted Results
print("\n------------- Report Card -------------")
print("Student Name :", student_name)
print("Roll Number :", roll_number)
print("Total Score :", total_marks, "/ 100")
print("Percentage :", str(percentage) + "%")

# 4. Conditional Check
if percentage >= 33.0:
    print("Result Status: PASSED")
else:
    print("Result Status: NEEDS IMPROVEMENT")
print("---------------------------------------")

Step 3: Run the Script
 * Save the file as student_report.py.
 * Press F5 in IDLE (or type python student_report.py in your terminal).
 * Enter the test details when prompted and verify the calculated output.
5. Lab Rules for Variable Naming (Best Practices)
To keep code clean and error-free, teach students these standard naming rules:
 * Variable names must begin with a letter (a–z, A–Z) or an underscore (_), never a number (e.g., student_1 is valid; 1student will raise a SyntaxError).
 * Variable names are case-sensitive (Score, score, and SCORE are three distinct variables).
 * Never use Python keywords (reserved terms like if, else, while, class, import) as variable names.
 * Use lowercase words separated by underscores for readability (snake_case), such as lab_computer_count.
6. Review Questions & Key Answers
Q1: What happens if you try to add an integer to a string in Python (e.g., 5 + "5")?
 * Answer: Python will raise a TypeError because it strictly forbids implicit mathematical operations between incompatible types. You must explicitly convert the string using int("5") or convert the number using str(5).
Q2: What is the purpose of the input() function in Python, and what data type does it return by default?
 * Answer: The input() function pauses execution to allow the user to type input from the keyboard. It always returns data as a String (str), which must be cast to an int or float if numerical calculation is required.
Q3: Why is Python called an "interpreted" language?
 * Answer: Python source code does not require a separate manual compilation step into machine code before running; instead, the Python interpreter reads and executes the script line by line at runtime.

Comments

Popular posts from this blog

विद्यार्थियों के लिए संस्कृत का अत्यंत प्रसिद्ध और महत्वपूर्ण श्लोक है।

MS Excel