0% found this document useful (0 votes)
16 views11 pages

Python Input and Data Types Guide

The document provides notes on Python input, data types, and loops for Class VIII students. It explains the use of the input() function, different data types (numbers, strings, lists), and how to convert input types. Additionally, it covers for and while loops, including examples and the range() function for generating sequences.

Uploaded by

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

Python Input and Data Types Guide

The document provides notes on Python input, data types, and loops for Class VIII students. It explains the use of the input() function, different data types (numbers, strings, lists), and how to convert input types. Additionally, it covers for and while loops, including examples and the range() function for generating sequences.

Uploaded by

steve.phillips
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🐍 Python Input & Data Types (Class VIII Notes)

1. Why Input is Needed?

 A program is more useful when it accepts data from the user.


 Python uses the input() function to take input.

2. Data Types

Python stores different kinds of data in different forms:

Data What it stores Example


Type
Number Integers or decimals 78, 190.34, -90
String Letters/characters inside quotes "Hello123", 'Welcome'
List Multiple items inside square ["Hello", 1, 2,
brackets "Welcome"]

3. The input() Function

Syntax:Taking a shower and getting ready and leavingPositive

x = input("Enter something: ")

 Shows the message to the user.


 Waits for the user to type something and press Enter.
 Stores the input in variable x.
 ⚠️By default, input is stored as a string.

4. Type Conversion (Changing Input Type)

Since input() gives a string, use these to convert:

 int() → changes input to an integer

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

 float() → changes input to decimal

price = float(input("Enter price: "))

 eval() → evaluates input as a number or expression (not safe for all cases)
5. Example Program

x = int(input("Enter a number: "))


print("Square of the number", x, "is", x**2)

Output:

Enter a number: 5
Square of the number 5 is 25

✅ Quick Recap:

 Use input() to get user input.


 Input is always a string.
 Convert to int or float when you need numbers.

🐍 Python Programs & Loops (Class VIII Notes – Page 2)

Example 2 – Divisibility Check

Program: Input a number and check if it is divisible by 5.

num = int(input("Enter a number: "))


if num % 5 == 0:
print(num, "is divisible by 5")
else:
print(num, "is not divisible by 5")

✔ If you type 89 → output: 89 is not divisible by 5

✔ If you type 505 → output: 505 is divisible by 5

Example 3 – Circle Area & Circumference

Program: Input radius of a circle and calculate area and circumference.

radius = float(input("Enter the radius of a circle: "))


area = 3.14 * radius * radius
circumference = 2 * 3.14 * radius
print("Area of a circle =", area)
print("Circumference of a circle =", circumference)

✔ If you type 2.5 →

Area = 19.625

Circumference = 15.7

Example 4 – Rectangle Area & Perimeter

Program: Input length and breadth of rectangle and calculate area and perimeter.

l = int(input("Enter length of a rectangle: "))


b = int(input("Enter breadth of a rectangle: "))
area = l * b
peri = 2 * (l + b)
print("Area of the rectangle =", area)
print("Perimeter of the rectangle =", peri)

✔ If you type 2 and 3.5 →

Area = 7.0

Perimeter = 11.0

🔁 Loop Statements

Loops allow us to repeat code without writing it again and again.

Python has two main loops:

 for loop
 while loop

1. for Loop

 Used to repeat actions over a range of numbers or a collection (like list, string).
 Format:

for i in range(start, end, step):


# code to run
👉 range() function:

 Generates numbers from start → end-1


 step tells how much to increase each time.

Example:

for i in range(1, 6):


print(i)

✔ Output: 1 2 3 4 5

✅ In summary for students:

 Example programs show how to take input and do calculations (divisibility, circle, rectangle).
 Loops (starting here) let you run code multiple times easily.
 range() is very important for writing for loops.

Got it 👍 Let’s simplify loop statements for Class VIII students:

🔁 Loops in Python (Simple Notes)

What is a Loop?

 A loop lets you repeat a set of instructions many times without writing them again and again.
 Example in real life: If teacher says “Write 1 to 10” → you don’t write 10 separate print() statements,
you use a loop.

1.
for Loop

👉 Used when you know how many times you want to repeat.

Format:

for i in range(start, end, step):


# code to run

 start → where to begin


 end → stops just before this number
 step → how much to increase each time
Example:

for i in range(1, 6):


print(i)

✔ Output: 1 2 3 4 5

2.
while Loop

👉 Used when you don’t know how many times to repeat in advance.

It runs while a condition is true.

Format:

while condition:
# code to run

Example:

i=1
while i <= 5:
print(i)
i=i+1

✔ Output: 1 2 3 4 5

✨ Quick Recap:

 for loop → repeat a fixed number of times.


 while loop → repeat until a condition becomes false.
 range() → helps create number sequences for for loops.

Perfect 👍 — this page is explaining the range() function with for loops in Python. Let’s simplify it so a
student can understand easily:

🔁 For Loops with

range()

in Python (Simple Notes)

General Form:
for i in range(start, end, step):
# code to run

 start → where the loop begins


 end → loop stops just before this number
 step → how much to increase each time (default = 1)

📝 Examples:

1. Basic loop (0 to 9)

for i in range(10):
print(i, end=' ')

✔ Output: 0 1 2 3 4 5 6 7 8 9

2. Loop with start and end

for i in range(2, 10):


print(i, end=' ')

✔ Output: 2 3 4 5 6 7 8 9

3. Loop with step (skipping numbers)

for i in range(2, 10, 3):


print(i, end=' ')

✔ Output: 2 5 8

4. Loop with negative step (counting backwards)

for i in range(10, 0, -2):


print(i, end=' ')

✔ Output: 10 8 6 4 2

📌 Important Notes:

 If step is not given, Python assumes it is 1.


 If start is not given, Python assumes it is 0.
 So:
o range(5) → counts 0 to 4
o range(2, 6) → counts 2 to 5
o range(2, 10, 2) → counts 2, 4, 6, 8

🔍 Examples from the sheet:

Example 1

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

✔ Output: 0 1 2 3 4

Example 2

for i in range(1, 5):


print(i, end=' ')

✔ Output: 1 2 3 4

Example 3

for i in range(1, 20, 2):


print(i, end=' ')

✔ Output: 1 3 5 7 9 11 13 15 17 19 (all odd numbers)

Example 4

for i in range(2, 11, 2):


print(i, end=' ')

✔ Output: 2 4 6 8 10 (all even numbers)

👉 Explanation: Here range(2,11,2) means start at 2, stop before 11, add +2 each time → 2, 4, 6, 8, 10.

✅ Quick Recap for Students:

 range() helps create sequences of numbers.


 for loop goes through each number one by one.
 Useful for printing, repeating tasks, or calculations.
Here’s a simple flowchart that explains how a for loop with range() works in Python.

This one put

It shows:

1. Start of loop
2. Taking first value of i
3. Condition check (i < end)
o If True → run the loop code
o If False → end the loop
4. Increase i by step
5. Repeat until finished

Got it 👍 Let me simplify the content of this page for students:

Loops in Python (Page Explanation)


Example 5 – Multiples of 5

for i in range(5, 51, 5): # Start at 5, go till 50, step = 5


print(i, end=' ')

Output:

5 10 15 20 25 30 35 40 45 50

👉 This prints the first 10 multiples of 5.

Example 6 – Print “Hello” 5 times

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

Output:

Hello

Hello

Hello

Hello

Hello

👉 The loop runs 5 times because range(5) means numbers 0,1,2,3,4 → total 5 times.

For Loop over Collection (Lists and Strings)

You can also use a for loop to go through lists, strings, tuples, or dictionaries.

 If it’s a list → loop runs through each item.


 If it’s a string → loop runs through each character.

Example 1 – Loop over a List

for i in ['amit', 12, 342]:


print(i, end=' ')

Output:
amit 12 342

👉 The loop prints each item from the list.

Example 2 – Loop over a String

for j in "Python":
print(j, end=' ')

Output:

Python

👉 The loop prints each character of the string one by one.

Example 3 – Colors of a Flag (List Example)

flag = ["Orange", "White", "Blue", "Green"]


print(flag, end='-')

Output:

Orange-White-Blue-Green

👉 The loop prints all items in the list separated by a -.

Example 4 – Print each letter of a Name

name = input("Enter your name: ")


for nm in name:
print(nm, end=' ')

Input:

Moana

Output:

Moana
👉 Each character of the name is printed separately.

Exercises

Q1. What is the output of this code?

for a in range(1,5):
print(a, end=' ')

Answer → 1 2 3 4

Q2. In Python, a ______ is a datatype that contains items separated by commas in square brackets.

Answer → List

✅ In short:

 range() is used for numbers.


 for … in is used for lists/strings.
 Loops repeat actions many times without writing the same code again.

You might also like