Python Tutorial

Erika Oliver

Erika Oliver

· 9 min read
Learn Python

What is Python?

Python is a high-level, interpreted popular programming language, known for its simplicity and readability.

What can Python do?

  • Python can be used in Web Development
  • Python can be used in Data Science and Machine Learning
  • Python can be used in Automation and Scripting
  • Python can be used in Desktop GUI Applications

Getting Started

There are three options to use python:

1. Install Python Locally: Go to the official python website https://www.python.org/ and follow instructions.

2. Use Python Online: You can use python on replit editor https://replit.com/languages/online-python-compiler

3. Use Python o Google Collab: If you are familiar with jupyter then you can use it online on google collab https://colab.research.google.com/

1. Hello World

The classic "Hello, World!" program in Python is a simple and great way to get started. Here it is:

Image

2. Control Flow

Control flow is an essential aspect of programming that allows you to make decisions and execute specific blocks of code based on conditions. In Python, this is achieved through if, else, and elif statements.

In this tutorial, we'll explore the fundamentals of control flow, boolean variables, and logical operators.

1. Boolean Variables:

Before diving into control flow, let's understand boolean variables. These variables can have only two values: True or False. They are often used to represent the result of a comparison.

Image

2. If-Else/Elif Statements:

The if statement is used to execute a block of code if a specified condition is true.

The elif (short for "else if") statement allows you to check multiple conditions.

The else statement is used in conjunction with if to execute a block of code when the condition in the if statement is false.

Image

3. Logical Operators:

Logical operators are used to combine multiple conditions.

  • and: Returns True if both conditions are true.
  • or: Returns True if at least one condition is true.
  • not: Returns True if the condition is false.
Image

Learn more about python control flow.

3. Lists

In Python, a list is a versatile data structure used to store an ordered collection of items. Lists are mutable, meaning you can modify their contents, and they can hold elements of different data types. This guide will cover the basics of working with lists, including creating, accessing, modifying, and manipulating list elements.

1. Creating Lists:

You can create a list by enclosing elements in square brackets [].

Image

2. Accessing Elements:

Elements in a list are accessed using index notation. Remember that Python uses 0-based indexing.

Image

3. Slicing Lists:

You can extract a portion of a list using slicing.

Image

4. Modifying Lists:

Lists are mutable, so you can change their elements.

Image

5. Adding and Removing Elements:

You can add elements to the end of a list using append() and remove elements using remove() or pop().

Image

Learn more about python lists.

4. Loops in Python

Loops in Python are structures that allow you to execute a block of code repeatedly. There are two main types of loops in Python: for loops and while loops. In this guide, we'll explore both types and provide examples to help you understand how to use loops effectively.

1. For Loops:

A for loop is used for iterating over a sequence (that is either a list, tuple, dictionary, string, or range).

  • Basic for Loop:
Image
  • Looping Over a Range:
Image
  • Looping Over a Dictionary:
Image

2. While Loops:

A while loop is used to repeatedly execute a block of code as long as the specified condition is true.

  • Basic while Loop:
Image
  • Infinite Loop:
Image

Learn more about python loops.

5. Functions in Python

Functions in Python allow you to encapsulate a block of code and reuse it, promoting modular and maintainable code. In this guide, we'll explore the basics of functions and then apply that knowledge to create functions for famous physics formulas.

1. Defining Functions:

To define a function in Python, use the def keyword, followed by the function name and parameters. The function body is indented.

Image

2. Calling Functions:

To execute a function, call it with the required arguments.

Image

3. Return Statement:

Functions can return values using the return statement.

Image

4. Physics Formulas Functions:

Now, let's create functions for some famous physics formula Newton's Second Law: F = m ⋅ a

Image

Learn more about python functions.

6. Strings in Python

Strings are a fundamental data type in Python, representing sequences of characters. They are versatile and support various operations, making them crucial for text processing. In this guide, we'll explore the basics of strings and demonstrate how to create, manipulate, and work with them effectively.

1. Creating Strings

You can create strings using single (') or double (") quotes.

Image

2. String Concatenation

Concatenate strings using the + operator.

Image

3. String Methods:

Python provides numerous built-in string methods for manipulating and analyzing strings.

Changing Case:

Image

Finding Substrings:

Image

Replacing Characters:

Image

You can learn more about string from python string documentation.

7. Modules in Python

In Python, a module is a file containing Python definitions and statements. Modules allow you to organize your code into reusable units, making it easier to manage and maintain. In this guide, we'll explore how modules work and provide examples to demonstrate their use.

1. Creating a Module

A module is simply a Python script containing functions, classes, and variables. Example: math_operations.py

Image

2. Using a Module

You can use the import statement to bring a module into your script and use its functions. Example: main_script.py

Image

3. Importing Specific Items

You can import specific functions or variables from a module. Example: main_script.py

Image

5. Built-in Modules

Python comes with many built-in modules that provide additional functionality. Example: main_script.py

Image

Learn more about Modules from python modules documentation.

8. Dictionaries in Python

Dictionaries in Python are versatile data structures that store key-value pairs. They allow you to efficiently organize and retrieve data based on unique keys. In this guide, we'll explore the basics of dictionaries and provide examples to help you understand how to create and manipulate them.

1. Creating Dictionaries

Dictionaries are defined using curly braces {} and consist of key-value pairs.

Image

2. Accessing Values

Access values in a dictionary using their keys.

Image

3. Modifying Dictionaries

Dictionaries are mutable, meaning you can modify their content.

Image

4. Dictionary Methods:

Python provides various built-in methods for working with dictionaries.

Image

Learn more about dictionaries in python dictionaries documentation.

9. Classes in Python

In Python, classes are used to create user-defined data types, allowing you to model real-world entities with attributes and behaviors. In this guide, we'll explore the fundamentals of classes, how to create objects from classes, and how to work with attributes and methods.

1. Creating a Class

Use the class keyword to create a class. Classes typically have attributes (characteristics) and methods (functions).

Image

2. Attributes and Methods

Attributes represent characteristics of an object, and methods are functions associated with the object.

Image

3. Constructor (__init__)

The __init__ method is a special method called when an object is created. It initializes the attributes of the object.

Image

4. Inheritance

Inheritance allows a class to inherit attributes and methods from another class.

Image

5. Encapsulation

Encapsulation involves restricting access to certain attributes or methods to prevent unintended interference.

Image

Learn more about classes from python classes documentation.

Erika Oliver

About Erika Oliver

Erika Oliver is a successful entrepreuner. She is the founder of Acme Inc, a bootstrapped business that builds affordable SaaS tools for local news, indie publishers, and other small businesses.

Copyright © 2024 Stablo. All rights reserved.
Made by Stablo