📘 Python Basics Book
By: Johan
🧾 Table of Contents
1. Introduction to Python
2. Variables and Data Types
3. Input and Output
4. Operators in Python
✅ CHAPTER 1: Introduction to Python
🔹 What is Python?
Python is a high-level, easy-to-read programming language used in fields like:
• Web development
• Machine learning
• Automation
• Game development
• Data analysis
Fact: Python was created by Guido van Rossum in 1991. It’s named after the British
comedy show “Monty Python”, not the snake!
🔹 Features of Python
Feature Description
Easy to Learn English-like syntax
Interpreted
Executes code line by line
Language
Cross-platform Runs on Windows, Linux, macOS
Dynamic Typing No need to declare data types
Versatile Used in many industries
Plenty of tutorials, libraries, and
Large Community
support
🔹 Example: Your First Program
# This prints a greeting
print("Hello, Python Learner!")
Output:
Hello, Python Learner!
📝 Try It Yourself
Write a Python program that prints:
Welcome to Python Basics Book!
✅ CHAPTER 2: Variables and Data Types
🔹 What are Variables?
Variables are used to store values. You can think of them as named boxes to hold data.
name = "John"
age = 17
🔹 Common Data Types in Python
Data Type Description Example Output
int Whole numbers x = 10 10
float Decimal values pi = 3.14 3.14
str Text/String msg = "Hi" Hi
True/False flag =
bool False
values False
🔹 Use Cases
• int → age, marks, quantity
• float → prices, percentages
• str → names, messages, user input
• bool → to make decisions
🔹 Typecasting Examples
x = int("5") # converts string to int
y = float("2.5") # string to float
z = str(10) # number to string
🔍 Differences Between Data Types
Criteria int float str bool
Whole
Stores Decimals Text True/False
numbers
Math
Yes Yes No Limited
operations
Measureme
Used for Counting Displaying Logic
nt
🧪 Mini Exercise
# Create a student's record
name = "Sara"
age = 16
marks = 95.5
passed = True
print(f"Name: {name}, Age: {age}, Marks: {marks}, Passed: {passed}")
✅ CHAPTER 3: Input and Output in Python
🔹 Input from User
Use input() to accept user input.
name = input("Enter your name: ")
print("Welcome,", name)
Output Example:
Enter your name: Riya
Welcome, Riya
By default, input returns a str. Convert to int/float if needed:
age = int(input("Enter your age: "))
🔹 Output with print()
print(\"Hello\") # Prints Hello
print(2 + 3) # Prints 5
print(\"Age:\", 17) # Prints Age: 17
You can also format text:
name = \"Alex\"
age = 18
print(f\"{name} is {age} years old.\")
🧪 Mini Exercises
• Ask the user for their favorite subject and print:
“Your favorite subject is ____.”
• Input 2 numbers and print their sum.
🔤 3. Operators
🔧 What are Operators?
Operators are special symbols used to perform operations on values and variables.
➕ 3.1 Arithmetic Operators
Operator Name Example Output Use Case
+ Addition 2 + 3 5 Combine numbers
- Subtraction 7 - 4 3 Find difference
* Multiplication 3 * 4 12 Repeated addition
/ Division 8 / 2 4.0 Decimal division
// Floor Division 8 // 3 2 Integer division
Modulus Remainder after
% 8 % 3 2
(Remainder) division
** Exponentiation 2 ** 3 8 Power of a number
✅ Use Case Example:
a = 10
b = 3
print("Sum:", a + b)
print("Quotient:", a // b)
print("Remainder:", a % b)
⚖️ 3.2 Relational Operators
Used to compare two values and return a boolean (True or False).
Operator Meaning Example Output
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5 > 3 True
< Less than 2 < 4 True
Greater or
>= 5 >= 5 True
Equal
<= Less or Equal 3 <= 3 True
🧠 3.3 Logical Operators
Used to combine multiple conditions.
Operator Meaning Example Output
Both must be 5 > 3 and 4
and True
true < 6
5 > 6 or 4 <
or At least one true True
6
not Inverts boolean not(5 > 3) False
💡 Mini Practice:
age = 20
citizen = True
if age >= 18 and citizen:
print("Eligible to vote")
else:
print("Not eligible")