0% found this document useful (0 votes)
4 views4 pages

Python Review Notes

The document provides an overview of basic Python programming concepts including variables, conditional statements, iterative statements, functions, and lists. It includes sample programs that demonstrate how to compute areas, determine pass/fail grades, and calculate averages. Each section explains the relevant concepts with code examples and expected outputs.

Uploaded by

rafolsjewels1
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)
4 views4 pages

Python Review Notes

The document provides an overview of basic Python programming concepts including variables, conditional statements, iterative statements, functions, and lists. It includes sample programs that demonstrate how to compute areas, determine pass/fail grades, and calculate averages. Each section explains the relevant concepts with code examples and expected outputs.

Uploaded by

rafolsjewels1
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

Review Notes and Sample Programs in Python

Topics Covered :
• Basic Python Programming
• Conditional Statements
• Iterative Statements
• Functions
• Arrays / Lists
1. Basic Python Programming
Python is a high-level programming language used to solve problems through clear and readable
instructions.
Basic Concepts
print("Hello, Engineering Students!")
print() displays output on the screen.
Variables
Variables store values.
length = 10
width = 5
area = length * width
print(area)
Python can store numbers, text, and results of computations.
Common data types:
int # whole number
float # decimal number
str # text
bool # True or False

1. Sample Program
Problem: Create a program that computes the area of the square.

Python Code Sample Output:


length = float(input("Enter the length: "))
Enter the length: 10
width = float(input("Enter the width: "))
Enter the width: 5
area = length * width
Area of Rectangle = 50.0
print("Area of Rectangle =", area)

Explanation :
Line 1 : Ask the user to input the length
Line 2 : Ask the user to input the width
Line 3: Compute the area
Line 4 : Display the command
2. Conditional Statements
Conditional statements allow a program to make decisions.
if condition:
statement
elif another_condition:
statement
else:
statement
Example:
grade = 85
if grade >= 75:
print("Passed")
else:
print("Failed")

2. Sample Program
Problem: Create a program that determines if a student passed or failed.

Python Code
grade = float(input("Enter your grade: ")) Sample Output:
if grade >= 75: Enter your grade: 82
print("PASSED")
PASSED
else:
print("FAILED")
Explanation :
Line 1 : Ask the user to input the grade
Line 2 : Check if the grade is passing
Line 3: Display the remarks “PASSED” if the grade is at least 75
Line 4 : Display the remarks “FAILED” if the grade is less than 77

3. Iterative Statements
Iterative statements repeat a set of instructions.

For Loop
Used when the number of repetitions is known.
for i in range(5):
print(i)

While Loop
Used while a condition is true.
count = 1

while count <= 5:


print(count)
count = count + 1

3. Sample statements
Problem: Create a program that displays numbers from 1 up to the user’s input
Python Code
# Ask the user for a number
Sample Output:
number = int(input("Enter a number: "))
Enter a number: 5
# Loop from 1 to the entered number 1
for i in range(1, number + 1): 2
print(i) 3
4
5
4. Functions
Functions are reusable blocks of code.
def function_name():
statements
Example:
def greet():
print("Welcome to Python Programming")

greet()
Functions help make programs organized and easier to understand.

4. Functions
Problem: Create a function that computes the area of a triangle.

Python Code
# Create a function Sample Output:
def triangle_area(base, height):
Enter the base: 12
# Compute the area Enter the height: 8
area = (base * height) / 2 Area of Triangle = 48.0

# Return the computed area


return area
# Ask the user to input values
base = float(input("Enter the base: "))
height = float(input("Enter the height: "))

# Call the function


result = triangle_area(base, height)

# Display the result


print("Area of Triangle =", result)

5. Arrays / Lists in Python


Python uses lists to store multiple values.
scores = [85, 90, 78, 92]
print(scores[0])
Indexing starts at 0.
Common list operations:
[Link](88) # adds a value
len(scores) # counts the values
sum(scores) # gets total
5. Arrays / Lists
Problem: Create a program that accepts 5 quiz scores and computes the average.

Python Code Sample Output:


# Create an empty list
scores = [] Enter score: 85
Enter score: 90
# Repeat 5 times
for i in range(5): Enter score: 88
Enter score: 92
# Ask the user to input a score Enter score: 87
score = float(input("Enter score: "))
Scores: [85.0, 90.0, 88.0, 92.0,
# Add the score to the list 87.0]
[Link](score)
Average Score = 88.4
# Compute the average
average = sum(scores) / len(scores)

# Display all scores


print("Scores:", scores)
# Display the average
print("Average Score =", average)

Sample Problem
A Civil Engineering student wants to compute the average compressive strength of concrete
samples. The program should:
1. Accept several strength values
2. Compute the average strength
3. Determine if the concrete passed the required strength of 21 MPa
Python Code
# This function computes the average value of the concrete strengths
def compute_average(strengths):
# sum(strengths) gets the total of all values in the list
# len(strengths) counts how many values are in the list
average = sum(strengths) / len(strengths)
# The computed average is returned to the main program
return average
# This list stores the compressive strength values in MPa
concrete_strengths = [22.5, 21.8, 20.9, 23.1, 22.0]
# The function is called and the result is stored in average_strength
average_strength = compute_average(concrete_strengths)
# This line displays the computed average strength
print("Average Concrete Strength:", average_strength, "MPa")
# This conditional statement checks if the average meets the required strength
if average_strength >= 21:
# This message is displayed if the condition is true
print("Result: PASSED")
else:
# This message is displayed if the condition is false
print("Result: FAILED")
Sample Output Simulation
Average Concrete Strength: 22.06 MPa
Result: PASSED

You might also like