Python Programming: An Introduction with Important Code Examples
Introduction
Python is one of the most popular programming languages in the world. It is a high-level, interpreted, and beginner-friendly programming language known for its simple and readable syntax.
Get Ranjitha D K’s stories in your inbox
Join Medium for free to get updates from this writer.
Python is used in many areas such as web development, data analysis, artificial intelligence, machine learning, automation, and software development. Because Python code is easy to understand, it is a good language for students who are starting their programming journey.
1. Printing Output
The print() function is used to display information on the screen.
print("Hello, World!")
print("Welcome to Python")Hello, World!
Welcome to Python2. Variables
Variables are used to store information. Python does not require us to specify the data type when creating a variable.
name = "Ranjitha"
age = 22
height = 5.4
print(name)
print(age)
print(height)Python automatically identifies the type of each value.
3. Taking User Input
The input() function allows a program to receive information from the user.
name = input("Enter your name: ")
print("Hello", name)For numbers, we can convert the input using int() or float().
age = int(input("Enter your age: "))
print("Your age is:", age)4. Basic Arithmetic Operations
Python can perform mathematical calculations easily.
a = 10
b = 5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Power:", a ** b)Some important operators are:
| Operator | Meaning |
| -------- | -------------- |
| `+` | Addition |
| `-` | Subtraction |
| `*` | Multiplication |
| `/` | Division |
| `//` | Floor division |
| `%` | Remainder |
| `**` | Power |5. Conditional Statements
Conditional statements allow a program to make decisions.
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult")
else:
print("You are a minor")We can also use elif when there are multiple conditions.
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")6. For Loop
A for loop is useful when we want to repeat a task a specific number of times.
for i in range(1, 6):
print(i)Output:
1
2
3
4
5Example: Multiplication Table
number = int(input("Enter a number: "))
for i in range(1, 11):
print(number, "x", i, "=", number * i)7. While Loop
A while loop continues running as long as a condition is true.
number = 1
while number <= 5:
print(number)
number += 18. Lists
A list is used to store multiple values in a single variable.
fruits = ["Apple", "Banana", "Mango"]
print(fruits)
print(fruits[0])We can add an item using append().
fruits.append("Orange")
print(fruits)We can also loop through a list.
for fruit in fruits:
print(fruit)9. Dictionaries
A dictionary stores information in key-value pairs.
student = {
"name": "Ranjitha",
"age": 22,
"course": "Python"
}
print(student["name"])
print(student["course"])Dictionaries are very useful when working with structured information.
10. Functions
A function is a reusable block of code.
def greet(name):
print("Hello", name)
greet("Ranjitha")
greet("Python Student")Functions help make programs organized, reusable, and easier to maintain.
Function with Return Value
def add(a, b):
return a + b
result = add(10, 20)
print("Result:", result)11. Exception Handling
Errors can occur while a program is running. Python provides try and except to handle errors.
try:
number = int(input("Enter a number: "))
print("You entered:", number)
except ValueError:
print("Please enter a valid number.")This prevents the program from stopping unexpectedly when the user enters invalid data.
12. Working with Files
Python can create, read, and write files.
Writing to a File
with open("example.txt", "w") as file:
file.write("Python is easy to learn.")Reading a File
with open("example.txt", "r") as file:
content = file.read()
print(content)13. Object-Oriented Programming
Python supports Object-Oriented Programming (OOP). A class can be used as a blueprint for creating objects.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def display(self):
print("Name:", self.name)
print("Course:", self.course)
student1 = Student("Ranjitha", "Python")
student1.display()Classes and objects are important when developing larger applications.
14. A Simple Python Project
Here is a small calculator program that combines several Python concepts.
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
if num2 != 0:
result = num1 / num2
else:
result = "Cannot divide by zero"
else:
result = "Invalid operator"
print("Result:", result)This simple project demonstrates input, variables, operators, conditions, and error checking.
Conclusion
Python is a powerful programming language that is relatively easy for beginners to learn. Its simple syntax allows students to focus on understanding programming concepts rather than complicated language rules.
Learning important concepts such as variables, data types, operators, conditional statements, loops, lists, dictionaries, functions, file handling, and object-oriented programming provides a strong foundation for further learning.
After learning Python basics, students can explore advanced areas such as data analysis, machine learning, artificial intelligence, web development, and automation.
Python is not only a programming language — it is a useful skill that can open the door to many different technology careers.







