Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python - N5

This document provides notes on Python variables and arrays, explaining their definitions, initialization, and usage. It covers how to print array elements, modify them, and use loops for various tasks, including checking for user input and calculating sums and averages. Additionally, it includes examples of creating a multiplication table and counting specific conditions within arrays.

Uploaded by

suresampats
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python - N5

This document provides notes on Python variables and arrays, explaining their definitions, initialization, and usage. It covers how to print array elements, modify them, and use loops for various tasks, including checking for user input and calculating sums and averages. Additionally, it includes examples of creating a multiplication table and counting specific conditions within arrays.

Uploaded by

suresampats
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Python Notes N5 - Variables and Arrays

Surender Samapath Date : 10/02/2025 V:1.0

What is a variable?

A variable is a named memory location that holds data that during the execution of a program, the data
can change

Variables can store a variety of different types of data such as integer, float, boolean.

Data initiailation: Set to empty.

a = 0

To store data in a variable, the process of assignment is used.

a = 10

What is an array?

Note:
Array : It is a collection of data items of same data type.
List : It is a collection of data items of different data type.
Index : It's the address or pointer to to access the element based on its position.
Print a particular elment in array :

a = ["Apple", "Banana", "Orange", "Grape"]

print(a[0]) # Output: Apple


print(a[1]) # Output: Banana
print(a[2]) # Output: Orange
print(a[3]) # Output: Grape

Assign/Modify a particular elment in array using it's index.

fruits = ["Apple", "Banana", "Orange", "Grape"]

# Modify the value at index 0.


fruits [0] = "Lemon"

print (fruits)

The above code will output

['Lemon', 'Banana', 'Orange', 'Grape']

✅ Arrays store multiple values in one variable.


✅ Indexing starts from 0.
✅ Arrays are accessed using [ ] - Square Brackets. Only for ARRAYS.
Printing a List Line by Line using len() and for Loop :

✅ What is a Loop?
A loop helps us repeat a set of lines without writing the same code again and again.

fruits = ["Apple", "Banana", "Orange", "Grape"]

fruits_length = len(fruits)

for i in range(fruits_length):
print(fruits[i])

How does it work?

1. len(fruits) returns the length of the list (4 items in this case).


2. range(fruits_length) ensures that the code will 4 times. Because length is 4.
3. for i in range(len(a)): loops through each index number. i value starts 0 and increments at
each loop. But not greater than range.
4. print(a[i]) prints the fruit at that index.

What do you need to loop through Array?

✅ i as an interator. Name can vary.

✅Length of Array to control the Loop.

✅[] to access the array using i .

📚 Check If a Fruit Exists in the Array (User Input) in Python

fruits = ["Apple", "Banana", "Orange", "Grape", "Blueberry"]

# Get user input


user_input = str(input("Enter the fruit name to check: "))
fruits_length = len(fruits)

# Loop through the list and check if the fruit starts with 'B'
for i in range(fruits_length):
if user_input == i:
print("Fruit is Found")
Print a multiplication table using FOR LOOP

Write a code to print a Multiplication table of 2 upt o 5 times.

# Multiplication table of 2 up to 5 times


num = 2
times = 5

for i in range(5):
print( num , "x" , i+1 , "=" , num * (i+1) )

The above code will output.

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10

Note: Can you do the a multiplication table as per user choice?

Explain: Why are adding 1 to i?

Question : Print first 20 odd numbers.

Running Total

Running total means adding values in the Array to the sum using a for loop.

A running total, also known as sum, is a calculation that progressively adds each value in a sequence to
the sum of all previous values. It represents a running or ongoing sum that updates with each new data
point.

✅ Sum variable is declared before the loop.

✅ Sum variable is printed after the loop.

To count sum of 5 numbers

# Running total of 5 numbers from USER.


sum_total = 0 # Initialise sum
total_input = 5 # Numbers to Add.
for i in range(total_input):
num = int(input("Enter a number: "))
sum_total += num # Add number to sum

print("Total Sum:", sum_total)

Calculate average height of students

# Sample array of student heights (in cm)


student_heights = [160, 152, 145, 172, 155]

# Calculate total number of students


num_students = len(student_heights)

# Initialise running total


total_height = 0

# Loop through the array to calculate running total


for i in range(num_students):
# Add each height to total.
total_height = total_height + student_heights[i]

# Compute average height


average_height = total_height / num_students

# Print the results using string formatting


print("Total Height:", total_height, "cm")
print("Average Height:", average_height, "cm")

Output for above program:

Total Height: 1601 cm


Average Height: 160.1 cm

Calculate the number of students taller than 150 cms.

# Sample array of student heights (in cm)


student_heights = [160, 152, 145, 172, 155]

# Calculate total number of students


num_students = len(student_heights)

# Initialise a variable counter


tall_students = 0
# Loop through the array and use criteria
for i in range(num_students):
# Using if to filter criteria
if student_heights[i]>150:
# Add each height to total.
tall_students = tall_students + 1

# Print the results using string formatting


print("Total Students above 150 cms:", tall_students)

Solve this below examples

Q1.

Q2.

Q3.
Q4.
Q5. Solve below

q6. Solve below

Line 1 SEND "Player One: Enter Number to Guess"


Line 2 RECEIVE target FROM KEYBOARD
Line 3 SET attempts TO 0
Line 4 WHILE (attempts < 10) AND (target ≠ guess) DO
Line 5 SEND "Enter your guess" TO DISPLAY
Line 6 RECEIVE guess FROM KEYBOARD
Line 7 IF guess > target THEN
Line 8 SEND "Too high" TO DISPLAY
Line 9 END IF
Line 10 IF guess < target THEN
Line 11 SEND "Too low" TO DISPLAY
Line 12 END IF
Line 13 END WHILE
Line 14 IF (target = guess) THEN
Line 15 SEND "Well done, you guessed correctly" TO DISPLAY
Line 16 END IF
Q7.

Q8.
Q9.

You might also like