Chapter 2 — Python Fundamentals
📚 Chapter Overview
Python is known for its simplicity. In this chapter, you will learn how Python handles data using dynamic typing, explore basic math and logic operations, and interact with users through the keyboard.
Learning Objectives:
- Understand Dynamic Typing and Variables.
- Master basic Data Types (Int, Float, String, Bool).
- Use
input()andformat()for interactivity.
2.1 Variables and Dynamic Typing
Part 1 — Definition
In Python, variables are created when you assign a value to them. Unlike Java or C++, Python is Dynamically Typed, meaning you don't need to specify the type of data. The interpreter figures it out automatically based on the value provided.
Part 2 — Syntax
name = "Intelle Learn"
age = 20
price = 19.99
is_active = True
Part 3 — Example
Problem: Swap two numbers without using a third variable.
a = 10
b = 20
a, b = b, a
print(f"a: {a}, b: {b}")
Output: a: 20, b: 10
Part 4 — Video
Video: Coming Soon
2.2 Python Input and Output
Part 1 — Definition
Interacting with users is essential for any script. The input() function reads text from the keyboard as a string. To treat it as a number, you must "Cast" it using int() or float(). Output is handled by the print() function.
Part 2 — Syntax
user_val = input("Enter your name: ")
age = int(input("Enter age: "))
print("Hello", user_val)
Part 3 — Example
Problem: Calculate the area of a circle based on user input.
radius = float(input("Enter Radius: "))
area = 3.14 * (radius ** 2)
print(f"The area is: {area:.2f}")
Output: (If radius is 5) The area is: 78.50
Part 4 — Video
Video: Coming Soon
🏆 Chapter Challenge
Challenge Objective
Build a "Smart Tip Calculator" that takes inputs and performs type casting and formatting.
Requirements
- Ask for the Bill Amount (float) and Tip Percentage (int).
- Calculate the Tip Amount and Total Bill.
- Display a formatted bill summary.
Step-by-Step Process
- Use
input()and cast to appropriate types. - Calculate
tip = bill * (tip_percent / 100). - Use f-strings to display the final amount.
Expected Deliverables
tip_calculator.pysource code.