0% found this document useful (0 votes)
5 views6 pages

Python Phase1 Lesson2 Input Arithmetic

This document is a beginner's guide to Python programming, specifically focusing on user input and arithmetic operations. It covers how to use the input() function, perform various arithmetic operations, and convert data types between strings, integers, and floats. Additionally, it provides examples, common mistakes, and practice exercises to reinforce learning.

Uploaded by

drk6284
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)
5 views6 pages

Python Phase1 Lesson2 Input Arithmetic

This document is a beginner's guide to Python programming, specifically focusing on user input and arithmetic operations. It covers how to use the input() function, perform various arithmetic operations, and convert data types between strings, integers, and floats. Additionally, it provides examples, common mistakes, and practice exercises to reinforce learning.

Uploaded by

drk6284
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 Mastery — Phase 1: Basics Input & Arithmetic

Python Mastery
Phase 1, Lesson 2 — Input & Arithmetic
A complete beginner's guide · Written for 10th grade and up

What you learned in Lesson 1: variables, data types (int, float, str, bool), and f-strings. Today you
build on that — you will get input from the user, do math in Python, and learn how to convert between
types.

1. Getting input from the user


Your programs so far only work with values you wrote yourself in the code. That's not very useful. The
input() function lets your program ask the user a question and store whatever they type.

How input() works


1. Python prints your question to the screen.
2. The program PAUSES and waits for the user to type something.
3. The user presses Enter.
4. Whatever they typed gets stored in your variable.

Basic input example


# The text inside the quotes is the prompt — what the user sees
name = input("What is your name? ")
print(f"Hello, {name}!")

# Output:
# What is your name? Arun ← user types this
# Hello, Arun!

Asking for multiple inputs


name = input("Enter your name: ")
city = input("Enter your city: ")
print(f"{name} lives in {city}")

# Output:
# Enter your name: Arun
# Enter your city: Chennai
# Arun lives in Chennai

Beginner's Python Notes | Phase 1 of 5 — Lesson 2 | Page 1


Python Mastery — Phase 1: Basics Input & Arithmetic

Remember: input() ALWAYS returns a string


Even if the user types 20, you get the string "20" — not the number 20.
You cannot do math with it directly. You must convert it first.
We fix this in Section 3 using int() and float().

2. Arithmetic operators
Python can do all kinds of math. There are 7 arithmetic operators. The first four are familiar — the last
three are new and very important.

Op Name Example Result What it does


+ Addition 7 + 3 10 Adds two numbers together.

- Subtraction 10 - 4 6 Subtracts right from left.

* Multiplication 4 * 5 20 Multiplies two numbers.

/ Division 10 / 4 2.5 Always returns a float (decimal).

// Floor division 10 // 3 3 Divides and drops the decimal part.

% Modulo 10 % 3 1 Returns the remainder after division.

** Exponent 2 ** 8 256 Raises left number to the power of


right.

All 7 operators in one program


a = 10
b = 3

print(a + b) # 13 — addition
print(a - b) # 7 — subtraction
print(a * b) # 30 — multiplication
print(a / b) # 3.3333... — division (always float)
print(a // b) # 3 — floor division (drops decimal)
print(a % b) # 1 — remainder (10 = 3x3 + 1)
print(a ** b) # 1000 — 10 to the power of 3

The three operators beginners always forget


// (floor division) — divides and throws away the decimal. 10 // 3 = 3.
% (modulo) — gives the REMAINDER after dividing. 10 % 3 = 1.
** (exponent) — raises to a power. 2 ** 10 = 1024.
All three come up constantly in LeetCode problems. Learn them now.

Order of operations — BODMAS still applies

Beginner's Python Notes | Phase 1 of 5 — Lesson 2 | Page 2


Python Mastery — Phase 1: Basics Input & Arithmetic

Python follows the same order of operations as maths. Use brackets to control the order.
print(2 + 3 * 4) # 14 — multiply first, then add
print((2 + 3) * 4) # 20 — brackets first
print(10 - 2 ** 3) # 2 — exponent first, then subtract
print(100 / 5 + 3) # 23.0 — divide first, then add

3. Converting types — int() and float()


This is one of the most important lessons for beginners. When you get a number from the user using
input(), it comes in as text (a string). You must convert it to a number before you can do any arithmetic.

Function Converts Converts to Example


from
int() string / float integer int("20") → 20 | int(9.9) → 9
float() string / int decimal float("3.14") → 3.14 | float(5) →
5.0
str() int / float string str(100) → "100" | str(3.14) →
"3.14"

What happens without conversion


# WRONG — this will crash
age = input("Enter your age: ") # age = "16" ← it's a string!
print(age + 1) # ERROR: cannot add str + int

The correct way


# CORRECT — wrap int() around input()
age = int(input("Enter your age: ")) # now age = 16 ← a real number
print(age + 1) # 17 — works!

Reading the pattern


int(input(...)) means: first run input() to get text, then run int() to convert it.
This is called NESTING — putting one function inside another.
Use int() for whole numbers (age, count, score).
Use float() for decimal numbers (price, weight, temperature).

4. Putting it all together — worked examples

Example 1 — Age in 2050 calculator


name = input("Your name: ")
age = int(input("Your current age: "))
in_2050 = age + (2050 - 2025)

Beginner's Python Notes | Phase 1 of 5 — Lesson 2 | Page 3


Python Mastery — Phase 1: Basics Input & Arithmetic

print(f"{name}, you will be {in_2050} years old in 2050.")

# Output:
# Your name: Arun
# Your current age: 16
# Arun, you will be 41 years old in 2050.

Example 2 — Bill calculator


price = float(input("Item price (Rs): "))
quantity = int(input("How many items? "))
subtotal = price * quantity
tax = subtotal * 0.18 # 18% GST
total = subtotal + tax

print(f"Subtotal : Rs {subtotal}")
print(f"Tax (18%): Rs {tax}")
print(f"Total : Rs {total}")

# Output:
# Item price (Rs): 250
# How many items? 4
# Subtotal : Rs 1000.0
# Tax (18%): Rs 180.0
# Total : Rs 1180.0

Example 3 — Simple average calculator


print("Enter your marks in 3 subjects:")
maths = float(input("Maths: "))
science = float(input("Science: "))
english = float(input("English: "))

average = (maths + science + english) / 3


print(f"Your average score is {average}")

5. Common mistakes

Mistake 1 — Forgetting to convert input()


Wrong: age = input("Age: ") then print(age + 1) — CRASH
Right: age = int(input("Age: ")) then print(age + 1) — works

Mistake 2 — Using int() on a decimal input


Wrong: price = int(input("Price: ")) — if user types 9.99, it crashes
Right: price = float(input("Price: ")) — float handles decimals correctly

Mistake 3 — Confusing / and //

Beginner's Python Notes | Phase 1 of 5 — Lesson 2 | Page 4


Python Mastery — Phase 1: Basics Input & Arithmetic

/ always gives a float: 10 / 2 = 5.0 (not 5)


// gives an integer (drops decimal): 10 // 2 = 5
Use // when you need a whole number result (e.g. how many full groups).

6. Practice exercises
Do these yourself before looking for answers. Type the code, run it, see what happens.

Exercise 1 — Temperature converter


Ask the user for a temperature in Celsius. Convert it to Fahrenheit using the formula: F = (C × 9/5) +
32. Print the result.

Exercise 2 — Rectangle area


Ask the user for the length and width of a rectangle. Calculate and print the area and the perimeter.

Exercise 3 — Modulo challenge


Ask the user for any number. Use % to check what the remainder is when divided by 2. (Hint: even
numbers give remainder 0, odd numbers give remainder 1.) Print the result.

Exercise 4 — Full calculator


Ask for two numbers. Print the result of all 7 operations (+, -, *, /, //, %, **) clearly labelled.

7. Quick reference card

# === INPUT ===


name = input("Prompt: ") # always returns string
age = int(input("Age: ")) # string → integer
price = float(input("Price: ")) # string → float

# === ARITHMETIC ===


a + b # addition
a - b # subtraction
a * b # multiplication
a / b # division — always gives float
a // b # floor division — drops the decimal
a % b # modulo — remainder after dividing
a ** b # exponent — a to the power of b

# === TYPE CONVERSION ===


int("20") # "20" → 20

Beginner's Python Notes | Phase 1 of 5 — Lesson 2 | Page 5


Python Mastery — Phase 1: Basics Input & Arithmetic

float("9.99") # "9.99" → 9.99


str(100) # 100 → "100"

# === FULL EXAMPLE ===


a = int(input("First number: "))
b = int(input("Second number: "))
print(f"Sum: {a + b} | Product: {a * b} | Remainder: {a % b}")

What comes next?


You have completed Lesson 2 of Phase 1. Here is the rest of your Phase 1 journey:

• Lesson 3 — If / elif / else: make your program take different actions based on conditions
• Lesson 4 — For loops & while loops: repeat actions without copy-pasting code
• Lesson 5 — Lists: store multiple values in a single variable

YouTube tip
Record yourself doing Exercise 4 — the full calculator.
Show the moment you realise you forgot to convert input() and it crashes.
Then show yourself fixing it. That 30-second clip is gold for beginners watching you.

Every program you will ever write takes input, processes it, and produces output. You just learned the
first two steps.

Beginner's Python Notes | Phase 1 of 5 — Lesson 2 | Page 6

You might also like