Welcome to Python 101! This guide is designed to introduce you to the basics of programming using the Python programming language. Python is a popular and versatile language known for its simplicity and readability. Whether you’re new to programming or have some experience with other languages, this guide will help you get started with Python.
- Installing Python: Before we begin, make sure you have Python installed on your computer. You can download the latest version of Python from the official website (https://www.python.org) and follow the installation instructions for your operating system.
- Python Interpreter: Python programs are executed using the Python interpreter. Once you have Python installed, you can open a terminal or command prompt and type
python
to start the interactive interpreter. This allows you to run Python code line by line and experiment with the language. - Writing Your First Program: Let’s start by writing a simple “Hello, World!” program. Open a text editor and create a new file with a
.py
extension, such ashello.py
. Type the following code:
pythonCopy codeprint("Hello, World!")
Save the file and open a terminal or command prompt. Navigate to the directory where you saved hello.py
and type python hello.py
. You should see the output Hello, World!
printed on the screen.
- Variables and Data Types: Python uses variables to store and manipulate data. You can assign a value to a variable using the
=
operator. Python supports different data types, including numbers, strings, lists, tuples, and dictionaries.
pythonCopy code# Numbers
x = 10
y = 3.14
# Strings
name = "Alice"
message = 'Hello, ' + name
# Lists
numbers = [1, 2, 3, 4, 5]
# Tuples
point = (10, 20)
# Dictionaries
person = {'name': 'Bob', 'age': 25}
- Basic Operations: Python provides various operators for performing arithmetic, comparison, and logical operations. Here are a few examples:
pythonCopy code# Arithmetic Operators
x = 10
y = 3
print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division
print(x % y) # Modulus
print(x ** y) # Exponentiation
# Comparison Operators
a = 5
b = 10
print(a == b) # Equal to
print(a != b) # Not equal to
print(a < b) # Less than
print(a > b) # Greater than
print(a <= b) # Less than or equal to
print(a >= b) # Greater than or equal to
# Logical Operators
p = True
q = False
print(p and q) # Logical AND
print(p or q) # Logical OR
print(not p) # Logical NOT
- Control Flow: Python provides control flow statements like
if
,for
, andwhile
to control the flow of execution in a program. Here’s an example:
pythonCopy codex = 5
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")
for i in range(5):
print(i)
while x > 0:
print(x)
x -= 1
- Functions: Functions are reusable blocks of code that perform a specific task. You can define your own functions in Python using the
def
keyword. Here’s an example:
pythonCopy codedef greet(name):
print("Hello, " + name)
greet("Alice")
- Libraries and Modules: Python has a vast ecosystem of libraries and modules that extend its functionality. You can import and use these libraries in your programs. For example, the
math
library provides mathematical functions:
pythonCopy codeimport math
print(math.sqrt(16))
print(math.pi)
- Resources: Learning Python is an ongoing process, and there are many resources available to help you further explore the language. Here are a few recommended resources:
- Official Python documentation: https://docs.python.org
- Python tutorials on w3schools: https://www.w3schools.com/python
- Python Crash Course by Eric Matthes
- Automate the Boring Stuff with Python by Al Sweigart
Congratulations! You’ve completed Python 101, a beginner’s guide to programming in Python. This guide should give you a solid foundation to continue your Python journey. Remember, practice is key to becoming a proficient programmer, so keep coding and exploring new concepts. Good luck!