Python is a programming language created in 1991 by Guido van Rossum. It's known for being simple to read and write, which makes it one of the most popular starting languages for beginners — while still being powerful enough for professional software, data science, and automation.
Python's syntax is close to plain English, so you spend less time fighting confusing symbols and more time actually solving problems. It's also extremely versatile — the same language can build websites, analyze data, automate boring tasks, or power AI models.
This page covers the core fundamentals of Python, step by step.
Python programs don't need a special starting function like
main() in C — code just runs from top to bottom, line by
line.A Python program usually contains comments, variables, input/output statements, functions, and the main program code.
Here's the simplest possible Python program:
print("Hello, World!")
# Output: Hello, World!
That's it — one line. No #include, no curly braces, no
semicolons. Python relies on indentation (spacing) instead of symbols
to organize code, which is a big part of why it's considered so easy
to read.
name = "Aravind"
print("Hello,", name)
# Output: Hello, Aravind
Here,nameis a variable storing the name "Aravind",and print() is a built-in function used to display output on the screen.
A variable is a name used to store a value in a Python program. The value of a variable can be changed during the program.
age = 21
price = 99.50
grade = 'A'
is_passed = True
int — whole numbers, like 10 or -5float — decimal numbers, like 3.14str — text, wrapped in quotes, like "hello"bool — either True or False
Since Python doesn't require you to state the type, you can check it
anytime using type().
age = 21
print(type(age))
# Output: <class 'int'>
age and Age are different variablesname = "Akshay"
age = 18
print(name, "is", age, "years old")
# Output: Akshay is 18 years old
A comment is a note in your code that Python completely ignores when running the program. It's just for humans — to explain what the code does, leave reminders, or temporarily disable a line without deleting it.
Use # — everything after it on that line is ignored.
# This prints a greeting
print("Hello, World!")
# Output: Hello, World!
Python doesn't have a true built-in multi-line comment symbol, but the common trick is to use triple quotes.
"""
This is a multi-line comment.
It can span several lines.
"""
print("Hello, World!")
# Output: Hello, World!
Input is the process of taking data from the user while a Python program is running, Output is the process of displaying information or results to the user.
Python uses print() to show output and input()
to take input from the user.
print()print("Hello, World!")
Hello, World!
input()
input() always returns text (a string), even if the user
types a number — so if you need it as a number, you have to convert it.
name = input("Enter your name: ")
print("Hello,", name)
"""Output:
Enter your name: Akshay
Hello, Akshay"""
age = input("Enter your age: ")
age = int(age)
print("You are", age, "years old")
# Output: Enter your age: 21
# You are 21 years old
int() converts text into a whole number. Use
float() instead if you're expecting a decimal.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(name, "is", age, "years old")
# Output: Enter your name: Akshay
# Enter your age: 21
# Akshay is 21 years old
input() always returns a string, even for numbersint() or float() to convert input when doing math with itprint() can display multiple values separated by commasOperators are symbols used to perform operations on values and variables.
+ — addition- — subtraction* — multiplication/ — division (always gives a decimal result)// — floor division (drops the decimal part)% — modulus (remainder after division)** — exponent (power)print(10 / 3) # 3.333...
print(10 // 3) # 3
print(10 % 3) # 1
print(2 ** 3) # 8
= — Assigns a value to a variable+= — Adds a value to the variable and stores the result.-= — Subtracts a value from the variable and stores the result.*= — Multiplies the variable by a value and stores the result./= — Divides the variable by a value and stores the result.%= — Finds the remainder and stores it in the variable.a = 10
a += 5 # a = 15
a -= 3 # a = 12
a *= 2 # a = 24
== — Checks if two values are equal.!= — Checks if two values are not equal.> — Checks if the first value is greater.< — Checks if the first value is smaller.>= — Checks if the first value is greater than or equal.<= — Checks if the first value is smaller than or equal.Python spells these out as words instead of symbols, unlike C/C++.
and — True if both conditions are trueor — True if at least one condition is truenot — Reverses the result (True becomes False and vice versa).age = 20
has_id = True
print(age >= 18 and has_id)
# True
Conditional statements are used to make decisions in a Python program based on whether a condition is True or False. Python doesn't use curly braces like C/C++; instead, it uses indentation to show which lines belong inside the condition.
if statementThe if statement executes a block of code only when the given condition is true.
age = 18
if age >= 18:
print("Adult")
# Output: Adult
Here, age >= 18 is checked. Since the condition is true, "Adult" is printed.
elseThe if-else statement executes one block when the condition is true and another block when it is false.
age = 15
if age >= 18:
print("Adult")
else:
print("Minor")
# Output: Minor
Since age >= 18 is false, the else block is executed. Therefore, "Minor" is printed.
The if-else statement executes one block when the condition is true and another block when it is false.
mark = 75
if mark >= 90:
print("Grade: A")
elif mark >= 75:
print("Grade: B")
else:
print("Grade: C")
# Output: Grade: B
Python first checks the if condition. If it is false, it checks elif; here, 75 >= 75 is true, so "B" is printed.
A nested if means having an if statement inside another if statement.
age = 20
if age >= 18:
if age >= 21:
print("Eligible")
# Output: (nothing is printed)
First, Python checks whether the age is at least 18. If true, it checks the second condition (age >= 21). Here, the second condition is false, so nothing is printed.
In Python, indentation isn't just for readability — it's part of the
syntax. If the spacing is wrong, the program will throw an error.
Always use consistent spacing (usually 4 spaces) under an if,
elif, or else.
Loops are used to repeat a block of code multiple times until a particular condition is met.
Loops allow the same block of code to be executed multiple times without writing it repeatedly.
for loopA for loop is used to iterate over a sequence such as a list, string, or range.
Best used when you want to repeat something a specific number of
times, often using range().
for i in range(1, 6):
print(i)
# Output: 1 2 3 4 5
i takes values from 1 to 5, and each value is printed.while loopA while loop repeatedly executes a block of code as long as the condition is true. Useful when you don't know in advance how many times it'll repeat.
i = 1
while i <= 5:
print(i)
i += 1
# Output: 1 2 3 4 5
i is less than or equal to 5.i += 1 increases the value after each iteration.break — stops the loop completelycontinue — skips the current round and moves to the next onefor i in range(1, 6):
if i == 3:
continue
print(i)
# prints: 1 2 4 5 (skips 3)
do-while loop like C/C++range(start, stop) stops one number before the given stop valueif statementsA function is a reusable block of code that performs a specific task. Instead of writing the same instructions repeatedly, you define them once and just call the function whenever you need it.
Use the def keyword, followed by a name and parentheses.
def greet():
print("Hello there!")
greet()
# Output: Hello there!
Parameters let you pass information into a function so it can work with different values each time.
def add(a, b):
return a + b
result = add(5, 3)
print(result)
# Output: 8
def — keyword used to define a functionadd(a, b) — the function name and the inputs it acceptsreturn a + b — sends the result back to wherever the function was calledSome functions just perform an action (like printing something) without needing to send anything back.
def show_message():
print("This function doesn't return anything.")
show_message()
# Output: This function doesn't return anything.
You can give a parameter a default value, used automatically if no value is passed in.
def greet(name="friend"):
print("Hello,", name)
greet()
greet("Akshay")
# Output: Hello, friend
# Hello, Akshay
A list is a collection used to store multiple values in a single variable. Lists are ordered, changeable, and allow duplicate values.
numbers = [10, 20, 30, 40, 50]
print(numbers)
# Output: [10, 20, 30, 40, 50]
Here, fruits is a list containing three items.
The items are stored in order and can be changed later.
Each item has a position called an index, starting from 0, just like arrays.
numbers = [10, 20, 30, 40, 50]
print(numbers[0])
print(numbers[2])
# Output: 10
# 30
numbers = [10, 20, 30, 40, 50]
numbers[1] = 99
print(numbers)
# Output: [10, 99, 30, 40, 50]
Unlike arrays, Python lists can grow — by using append() to
add a new item to the end.
numbers.append(60)
print(numbers)
# Output: [10, 99, 30, 40, 50, 60]
numbers = [10, 99, 30, 40, 50]
numbers.remove(99)
print(numbers)
# Output: [10, 30, 40, 50, 60]
for num in numbers:
print(num)
# Output: 10
# 30
# 40
# 50
# 60
append() and remove()Think you've got Python down? Test yourself!
Start Quiz →