Welcome to the basics of Python programming! Python is a versatile and popular programming language used for various applications, from web development to data analysis.

Getting Started

Before diving into Python, make sure you have Python installed on your system. You can download and install Python from the official Python website.

Hello World

Your first Python program should be a simple "Hello World" script. Here's an example:

print("Hello, World!")

Save this script as hello_world.py and run it using the Python interpreter. You should see the following output:

Hello, World!

Variables and Data Types

In Python, variables are used to store data. Python has several data types, including integers, floating-point numbers, strings, and booleans.

Variables

Variables are defined using the assignment operator (=). For example:

name = "John Doe"
age = 30
is_student = False

Data Types

  • String: A sequence of characters. For example, "Python".
  • Integer: A whole number. For example, 5.
  • Float: A number with a decimal point. For example, 3.14.
  • Boolean: A value that can be either True or False.

Control Structures

Python has various control structures for decision-making and repetition.

If-Else

if age > 18:
    print("You are an adult.")
else:
    print("You are not an adult.")

For Loop

for i in range(5):
    print(i)

While Loop

count = 0
while count < 5:
    print(count)
    count += 1

Functions

Functions are blocks of code that can be reused. They help in organizing and structuring your code.

Defining a Function

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")

Parameters and Arguments

Functions can have parameters and arguments. Parameters are variables defined in the function definition, and arguments are the values passed to the function when it is called.

Lists

Lists are ordered collections of items. They can contain different types of data.

Accessing Elements

my_list = [1, 2, 3, 4, 5]
print(my_list[0])  # Output: 1

Adding Elements

my_list.append(6)
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

List Comprehensions

List comprehensions provide a concise way to create lists.

squares = [x ** 2 for x in range(1, 6)]
print(squares)  # Output: [1, 4, 9, 16, 25]

Dictionaries

Dictionaries are unordered collections of key-value pairs.

Accessing Elements

my_dict = {"name": "John", "age": 30}
print(my_dict["name"])  # Output: John

Adding Elements

my_dict["city"] = "New York"
print(my_dict)  # Output: {'name': 'John', 'age': 30, 'city': 'New York'}

Modules and Packages

Python has a vast ecosystem of modules and packages for various tasks.

Importing Modules

import math

print(math.sqrt(16))  # Output: 4.0

Importing Specific Functions

from math import sqrt

print(sqrt(25))  # Output: 5.0

For more information on modules and packages, check out the Python documentation.

Conclusion

Congratulations! You've learned the basics of Python programming. Continue exploring and expanding your knowledge by experimenting with the code examples and reading more about Python.

Continue Learning Python