KEMBAR78
Class VIII - Notes of Python Program | PDF | Python (Programming Language) | Control Flow
0% found this document useful (0 votes)
10 views6 pages

Class VIII - Notes of Python Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Class VIII - Notes of Python Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

📌St.

Xavier’s Institution, Panihati


CLASS VIII

Additional Notes on
Python Programming
Take aways and Solved Programs

JAN 2025

ISSUED BY
Robotics & AI Teachers, SXI Panihati
Introduction
Overview:

Python is a high-level, interpreted, and general-purpose programming language known for its
simplicity and readability.

Key Features and Advantages:

● Easy to Learn and Use: Simple syntax and beginner-friendly.


● Interpreted: No need for compilation; runs directly.
● Dynamically Typed: No need to declare variable types.
● Extensive Libraries: Libraries for diverse tasks (e.g., NumPy, Pandas, Matplotlib).
● Platform Independent: Works across different operating systems.
● Community Support: Large and active community.

Applications of Python:

● Web Development (e.g., Django, Flask)


● Data Science and Machine Learning
● Scripting and Automation
● Game Development
● Internet of Things (IoT)

Python Modes
Interactive Mode: Script Mode:
● Used to write and execute complete
● Allows executing Python commands Python programs.
line-by-line. ● Python scripts are saved with the .py
● Useful for quick testing and extension.
debugging.
Example:
Example: # script.py
>>> print("Hello, Python!") print("Hello, Python!")

Hello, Python! Run using: python script.py

1
Basic Syntax
Data Types: Operators:
● Common data types: int, float, str, ● Arithmetic: +, -, *, /, //, %, **
bool, list, tuple, dict, set. ● Comparison: ==, !=, <, >, <=, >=
● Logical: and, or, not
Example:
Example:
a=5 # int x, y = 10, 3
b = 3.14 # float print(x // y) # 3
c = "Hello" # str print(x % y) # 1
d = True # bool print(x>y>0) # True
Variables: Output (Print):
● Variables are containers for storing
data values. Same data types:
● No need for explicit declaration. print(“Hello world!")
print(10)
● Can only start with alphabet and
print(“Hello ” + “world!")
underscore (_) print(10+12)

Syntax: Multiple data types:


variable_name = value name = "Amit"
age = 10
Example: print("Name: ", name, "Age: ", age)
_x = 10 ✅ # format()
y = "Python" ✅ print("Name: {}, Age: {}".format(name, age))
# f-strings
x8 = 10 ✅ print(f"Name: {name}, Age: {age}")
8x = 10 ❌ # % operator
print("Name: %s, Age: %d" % (name, age))

More Examples:
x,y = 10,0
print(x and y) # 0
# Explanation: the "and" operator returns the first "falsy" value encountered. Since "y" is 0 (considered
"falsy"), the expression "x and y" evaluates to 0 even though "x" is 10. If all values are “truthy”, it returns
the last evaluated value. “Falsy” values that behave like False (0, "", [], None, False).

print(x or y) # 10
# Explanation: The "or" operator returns the first "truthy" value encountered. Since "x" is 10 (considered
"truthy"), the expression "x or y" evaluates to 10. If all values are “falsy”, it returns the last evaluated value.
“Truthy” values that behave like True (non-zero numbers, non-empty objects).

2
Structure of Python Programs
Components of a Python Program:
1. Comments: Used to describe code. Single-line (#), Multi-line (''' or """).
2. Statements: Instructions executed by Python.
3. Indentation: Defines code blocks (e.g., in loops, functions).
Writing and Running Python Scripts:
● Write the code in a .py file.
● Execute the script using python filename.py.
Example Script:
name = input("Enter your name: ")
print(f"Hello, {name}!")

Errors and Debugging


Common Error Types:
● SyntaxError: Incorrect syntax.
● IndentationError: Incorrect indentation.
● NameError: Undefined variable.
● TypeError: Operation on incompatible types.
● Other errors.

Debugging Techniques:
● Use print() statements to check variable values.
● Write modular code to isolate issues.

Control Structures in Python


Types of Control Structures

Sequential Control Statements:

● Executes code line-by-line.

Conditional Control Statements:

3
● Conditional statements allow a program to make decisions based on conditions.

Example:

if x > 0:
print("Positive")
else:
print("Non-positive")
# Notice: the colon (:) after if/else statement and the indentation in the following line

Iterative Control Statements:

● Loops to execute code multiple times.

Example:

for i in range(5):
print(i)

# range(start, stop + (1 or -1 for increasing or decreasing order), increment): This function


generates a sequence of numbers. By default, it starts at 0, and the increment is set to 1 unless
specified otherwise. range(5) = 0,1,2,3,4

Solved Examples
1. Write a program using a function to calculate the simple interest using the formula “Simple
Interest = PRT/100”.
# Take inputs from the user
principal = float(input("Enter your principal amount: "))
int_rate = float(input("Enter the interest rate: "))
time = float(input("Enter the investment time (in years): "))
simple_interest = (principal * int_rate * time) / 100
print(simple_interest)

2. Write a function that takes an integer n and returns the first n numbers in a sequence (1 to
n).
n = int(input("Enter an integer number: "))
# ‘n’ terms will be printed
for i in range(1,n+1):
print(i)
# range(start=1, stop=n, increment=1(default))

4
3. Write a Python program to add 4 numbers by taking them as inputs.
print("Addition of four numbers")
total = 0
for i in range(1,5): # Loop from 1 to 4 (4 iterations)
num = float(input("Enter a number: ")) # Take a number as input
total = total + num # Add the input number to the total
print("Total:", total)
# range(start=1, stop=4 (excludes the last element), increment=1(default))

4. Write a Python program to compare three numbers (taken as input) and determine the
greatest among them.
a = int(input("Enter values respectively: "))
b = int(input("Enter values respectively: "))
c = int(input("Enter values respectively: "))
if (a<b and c<b): # here b is the greatest
print(b,"is the greatest")
elif (c<a): # here b can’t be the greatest, either c or a
print(a,"is the greatest")
else:
print(c,"is the greatest")

5. Write a program that prints the numbers from 1 to 100. Print "Fizz" instead of the number
for multiples of three, and for multiples of five, print "Buzz." For numbers that are multiples
of both three and five, print "FizzBuzz."
for i in range(1, 101): # Loop from 1 to 100 (100 iterations)
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

You might also like