Introduction to Python
What is Python?
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 emphasizes code readability, making it an excellent choice for beginners and experienced developers alike.
Key Features of Python
- Readability: Python uses indentation to define code blocks, which makes the code easier to read and understand.
- Interpreted Language: Python code is executed line by line, which allows for quick testing and debugging.
- Versatility: Python can be used for web development, data analysis, artificial intelligence, scientific computing, and more.
- Large Standard Library: Python comes with a vast collection of modules and packages that simplify various tasks.
Basic Syntax
Python syntax is designed to be intuitive. Here’s a simple example of a Python program that prints "Hello, World!":
print("Hello, World!")
In this example, the print()
function is used to output the text "Hello, World!" to the console.
Variables and Data Types
In Python, you don’t need to declare variables with a specific type. Python automatically determines the type based on the value assigned. Here’s an example:
x = 10 # x is an integer y = "Hello" # y is a string z = 3.14 # z is a float
Python supports various data types such as integers, floats, strings, and booleans. You can also use built-in functions like type()
to check the type of a variable:
print(type(x)) # Output: <class 'int'> print(type(y)) # Output: <class 'str'> print(type(z)) # Output: <class 'float'>
Control Flow
Python provides control flow statements like if
, else
, and elif
to make decisions in your code. Here’s an example:
age = 18 if age < 18: print("You are a minor.") elif age == 18: print("You just turned 18!") else: print("You are an adult.")
In this example, the program checks the value of age
and prints the appropriate message based on the condition.
Loops
Python supports two types of loops: for
loops and while
loops. A for
loop is used to iterate over a sequence, while a while
loop continues to execute as long as a condition is true. Here’s an example:
# For loop example for i in range(5): print(i) # While loop example count = 0 while count < 5: print(count) count += 1
In the first example, the for
loop iterates over the range from 0 to 4. In the second example, the while
loop continues to print the value of count
until it reaches 5.
Functions
Functions in Python are defined using the def
keyword. They allow you to encapsulate a block of code that can be reused. Here’s an example:
def greet(name): print("Hello, " + name + "!") greet("Alice") # Output: Hello, Alice! greet("Bob") # Output: Hello, Bob!
In this example, the greet
function takes a parameter name
and prints a greeting message.
Conclusion
Python is a powerful and versatile language that is easy to learn and use. Its simplicity and readability make it an excellent choice for both beginners and experienced programmers. By understanding the basics of Python, you can start building your own programs and exploring more advanced topics.