Welcome to the Python Basics Tutorial! This guide will cover the fundamental concepts and syntax of the Python programming language. Python is known for its simplicity and readability, making it a great choice for beginners and experienced programmers alike.
Installation
Before you start, make sure you have Python installed on your system. You can download the latest version from the official Python website.
Hello World
Let's begin with a classic: the "Hello World" program. This program is a staple for learning any programming language.
print("Hello, World!")
This code will display the message "Hello, World!" in the console.
Variables
In Python, variables are used to store data. Here's an example:
name = "Alice"
age = 25
In this example, name
and age
are variables. The value of name
is "Alice", and the value of age
is 25.
Data Types
Python has several built-in data types, including strings, integers, floats, and booleans.
- Strings: Textual data
message = "Hello, Python!"
- Integers: Whole numbers
number = 42
- Floats: Numbers with decimals
pi = 3.14159
- Booleans: True or False
is_valid = True
Lists
Lists are a collection of items in a specific order. You can create a list using square brackets []
.
fruits = ["apple", "banana", "cherry"]
You can access elements in a list using their index:
print(fruits[0]) # Output: apple
Control Flow
Control flow is how the program executes code based on conditions. Here's an example using if
statements:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are not an adult.")
Functions
Functions are blocks of code that perform a specific task. Here's an example of a function that calculates the square of a number:
def square(number):
return number * number
result = square(5)
print(result) # Output: 25
Further Reading
For more in-depth learning, check out our Advanced Python Tutorial.