Python is a high-level, interpreted programming language known for its simplicity and readability. It was created by Guido van Rossum and first released in 1991. Python is widely used in various fields such as web development, data science, artificial intelligence, and more.
Here is a simple "Hello, World!" program written in Python:
print("Hello, World!")
Python supports various data types, including:
Python provides various control flow statements to control the flow of execution in a program:
Functions in Python allow you to encapsulate reusable pieces of code. Here is an example of a simple function that adds two numbers:
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result) # Output: 8
Python allows you to organize code into modules and packages for better code organization and reusability. Modules are files containing Python code, while packages are directories containing multiple modules.
Python supports object-oriented programming (OOP) concepts such as classes, objects, inheritance, and polymorphism. Here is an example of a simple class in Python:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is", self.name)
# Create an object of the Person class
person1 = Person("Alice", 30)
person1.greet() # Output: Hello, my name is Alice
Python allows you to handle exceptions using try-except blocks to gracefully handle errors in your code:
try:
result = 10 / 0
except