Chapter 1: Programming in Python (Basics, Variables, Data Types)

By the end of the lesson “Programming in Python”, readers will:

  • Understand the Concept of Python Programming
  • Setting Up Python
  • Basic Terms Comments, Variables, Data Types, etc.
  • Understand Operators in Python
  • Basic Code
  • Solve Some Practice Questions

Python is a high-level, interpreted programming language known for its simplicity and readability. It is widely used in web development, data science, artificial intelligence, and automation.

  • Easy to learn and write
  • Interpreted language (Executes line by line)
  • Supports multiple paradigms (Procedural, Object-Oriented, Functional)
  • Extensive library support
  • Simple syntax similar to English
  • Used by companies like Google, Facebook, NASA
  • Great for beginners and professionals

Example of a simple Python program:

print("Welcome to Python Programming!")

This will output: Welcome to Python Programming!

To start coding in Python, you need to install Python and use an IDE (Integrated Development Environment) like:

  • IDLE (Comes with Python)
  • VS Code
  • PyCharm
  • Jupyter Notebook

Python code can be written and executed in three ways:

  • Using Python Interpreter (IDLE)
  • Using a Python Script (.py file)
  • Using an Online Compiler (Google Colab, Replit, etc.)

Comments are used to explain the code and are ignored by the interpreter.

1. Single-Line Comment:

# This is a single-line comment
print("Hello, Python!")  # This prints a message

2. Multi-Line Comment:

For multi-line comments, use triple quotes:

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

A variable is a name given to a memory location where data is stored.
πŸ’‘ Python is dynamically typed (No need to declare the type explicitly).

Declaring Variables

name = "Alice"  # String
age = 12        # Integer
height = 5.4    # Float
is_student = True  # Boolean
  • Variable names should start with a letter (A-Z, a-z) or underscore (_)
  • Should not use keywords like print, class, if, etc.

Python provides several data types to store different kinds of values.

Data type in Programming in Python Class 7 Computer Science Notes (www.jngacademy.com)

Example:

x = 10        # Integer
y = 3.14      # Float
name = "John" # String
is_valid = True # Boolean
fruits = ["Apple", "Banana", "Cherry"]  # List

Python allows conversion of one data type to another.

Implicit Type Conversion

Python automatically converts a smaller data type to a larger data type.

num = 5   # Integer
num = num + 2.5  # Now it becomes float (7.5)
print(type(num))  # Output: <class 'float'>

Explicit Type Conversion

We can manually convert types using int(), float(), str(), bool().

a = "123"
b = int(a)  # Convert string to integer
print(b)  # Output: 123
  • input() function is used to take user input.
  • print() function is used to display output.

Example:

name = input("Enter your name: ")
print("Hello, " + name + "!")

If the user enters “Adam”, the output will be:
Hello, Adam!

Operators are used to perform operations on variables and values.

1. Arithmetic Operators

Arithmetic Operators in Python (www.jngacademy.com)

Example:

a = 10
b = 5
print(a + b)  # Output: 15
print(a ** b)  # Output: 100000 (10^5)

2. Comparison Operators

a = 10
b = 20
print(a == b)  # False
print(a != b)  # True
print(a < b)   # True
print(a > b)   # False

3. Logical Operators

x = True
y = False
print(x and y)  # False
print(x or y)   # True
print(not x)    # False

Conditional statements allow decision-making in programs.

age = 18
if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

if-elif-else Example

marks = 85
if marks >= 90:
    print("Grade: A+")
elif marks >= 75:
    print("Grade: A")
else:
    print("Grade: B")

Loops are used to execute a block of code multiple times.

For Loop (Definite Iteration)

for i in range(1, 6):
    print(i)  # Output: 1 2 3 4 5

While Loop (Indefinite Iteration)

num = 1
while num <= 5:
    print(num)  # Output: 1 2 3 4 5
    num += 1

Loop Control Statements

  • break – Exit the loop
  • continue – Skip the current iteration
  • pass – Placeholder

Example:

for i in range(1, 6):
    if i == 3:
        continue  # Skips 3
    print(i)  # Output: 1 2 4 5
  • Python is an easy and powerful language
  • Variables store data dynamically
  • Different data types: int, float, str, bool, list, tuple, dict
  • Type conversion allows changing data types
  • Operators perform mathematical operations
  • Variables & Data Types – Store and manage data.
  • Operators – Perform calculations & comparisons.
  • Conditionals & Loops – Control program flow.
  • Functions – Reuse code.
  • Lists, Tuples, Dictionaries – Store and manage collections.
  • File Handling – Read and write files.
  • Exception Handling – Manage errors.
  • OOP Concepts – Organize code into reusable classes.

  • Created by Guido van Rossum – Python was developed in 1989 and released in 1991.
  • Named after Monty Python – The name “Python” was inspired by the British comedy show Monty Python’s Flying Circus, not the snake!
  • Interpreted Language – Python executes code line by line, making debugging easier.
  • Dynamically Typed – No need to declare data types; Python automatically assigns them.
  • Indentation Matters – Unlike other languages that use {} or ;, Python uses indentation (spaces/tabs) to define blocks of code.
  • Multi-Purpose Language – Python is used in AI, web development, data science, automation, cybersecurity, gaming, and more!
  • Huge Library Support – Python has thousands of built-in libraries like numpy, pandas, matplotlib, tensorflow, and django for various applications.
  • Cross-Platform Compatibility – Python runs on Windows, macOS, Linux, and even mobile devices!
  • Most Popular Programming Language – According to the TIOBE Index, Python is consistently ranked as the #1 most used language.
  • Used by Tech Giants – Python is used by Google, Facebook, Instagram, NASA, Netflix, YouTube, and even NASA for space missions!
  • Simple Yet Powerful – Python’s syntax is similar to English, making it beginner-friendly while still being powerful enough for complex applications.
  • Supports Object-Oriented and Functional Programming – Python allows both OOP and functional programming, giving developers flexibility.
  • Large Community Support – Being open-source, Python has a massive global community, making it easy to find solutions and resources online.
  • Python is used in Machine Learning & AI – Libraries like TensorFlow, Scikit-learn, and PyTorch make it the top choice for AI research.
  • Famous Apps Built with Python – Apps like YouTube, Instagram, Spotify, Reddit, and Dropbox are built using Python.
programming-in-python-class-7-computer-notes(www.jngacademy.com)

1. Print your name, age, and favorite hobby using separate print() statements.

print("Hello, Python!")
print("My name is Laiba.")
print("I am 14 years old.")
print("My favorite hobby is painting.")

2. Declare three variables: name, age, and height, and print them.

# Assign values to variables
name = "Laiba"
age = 14
height = 5.4

# Print variables
print(name, age, height)

# Check data types
x = 10       # Integer
y = 5.5      # Float
z = "Hello"  # String

print(type(x))  # Output: <class 'int'>
print(type(y))  # Output: <class 'float'>
print(type(z))  # Output: <class 'str'>

3. Write a program that asks the user for their name and age and then prints a message like:
"Hello, Adam! You are 12 years old."

name = input("Enter your name: ")
age = input("Enter your age: ")

print("Hello,", name + "! You are", age, "years old.")

4. Write a Python program to add, subtract, multiply, and divide two numbers entered by the user.

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Remainder when 15 is divided by 4:", 15 % 4)

5. Convert the string "123" to an integer and multiply it by 2.

x = 10
y = str(x)  # Convert integer to string
print(y, type(y))

s = "123"
num = int(s)  # Convert string to integer
print(num * 2)  # Output: 246

6. Write a program to check if a number is even or odd.

# Check even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
    print(num, "is Even")
else:
    print(num, "is Odd")

# Check voting eligibility
age = int(input("Enter your age: "))
if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

7. a. Print numbers from 1 to 10 using a for loop.

# Print numbers from 1 to 10 using a for loop
for i in range(1, 11):
    print(i)

b. Print the multiplication table of a number entered by the user.

# Multiplication table of a number
n = int(input("Enter a number: "))
for i in range(1, 11):
    print(n, "x", i, "=", n * i)

c. Use a while loop to print numbers from 10 to 1.

# Print numbers from 10 to 1 using while loop
x = 10
while x >= 1:
    print(x)
    x -= 1

8. a. Create a list of five favorite fruits and print the second fruit.

# List example
fruits = ["Apple", "Banana", "Mango", "Grapes", "Orange"]
print("Second fruit:", fruits[1])

b. Create a tuple with three numbers and print their sum.

# Tuple example
numbers = (10, 20, 30)
print("Sum of tuple numbers:", sum(numbers))

9. a. Create a dictionary with three student names as keys and their ages as values.

students = {"Laiba": 14, "Tanzeel": 15, "Shahla": 13}
print("Age of Tanzeel:", students["Tanzeel"])

b. Retrieve and print the age of a student from the dictionary.

students = {"Laiba": 14, "Tanzeel": 15, "Shahla": 13}
print("Age of Tanzeel:", students["Tanzeel"])

10. a. Write a function to find the sum of two numbers.

# Function to find sum of two numbers
def add(a, b):
    return a + b

print("Sum:", add(5, 7))

b. Write a function to find the sum of two numbers.

# Function to print a greeting message
def greet(name):
    print("Hello,", name + "!")

greet("Laiba")

Coming Soon…

Provide downloadable materials for learners to review:

  • – PDF Guide: “Coming Soon”
  • – Cheat Sheet: “Coming Soon”
  • – Video Source: “JNG ACADEMY
  • – Articles: “Blog Page

Q1: What is Python?

Python is a high-level, easy-to-learn programming language used in various fields.

Q2: What is a variable in Python?

A variable is a container that holds data.

Q3: What are the main data types in Python?

int, float, string, boolean, list, tuple, dictionary.

Q4: What is the difference between a list and a tuple?

A list is mutable (can change), while a tuple is immutable (cannot change).

Q5: What is type conversion?

Converting a variable from one data type to another.

Paid Earning Courses:

Leave a Comment