Python Tutorial in One Hour: A Beginner’s Guide to Python Programming

CodeWithMuh
Code With Muh
Published in
6 min readFeb 3, 2023
https://www.youtube.com/@codewithmuh

Python is a high-level, interpreted, general-purpose programming language that has become increasingly popular in recent years due to its simplicity, versatility, and ease of use. Whether you’re a beginner or an experienced programmer, Python is a great language to learn. In this tutorial, we’ll provide a comprehensive overview of Python programming basics, covering all the key concepts you need to know in just one hour.

Introduction to Python

Python was created in the late 1980s by Guido van Rossum, and its name was inspired by the Monty Python comedy group. The Python Software Foundation now maintains it and has a huge following of users and developers worldwide. Python is often referred to as a “batteries included” language because it comes with many built-in modules and libraries that provide functionality for a wide range of tasks, from web development to scientific computing.

https://www.youtube.com/watch?v=9q6XbhLawRI

Why use Python?

  • Easy to learn: Python’s syntax is simple, clean, and readable, making it easy for beginners to learn the language.
  • Versatile: Python can be used for various tasks, from web development and data analysis to scientific computing and artificial intelligence.
  • Large community: Python has a large and active community of users and developers, making it easy to find help and support.
  • Plenty of libraries: Python has many built-in libraries and modules, making it easy to perform complex tasks without writing a lot of code.

How to install Python?

Python is available for Windows, macOS, and Linux and can be easily installed by downloading the official Python installer from the official Python website. If you’re using Windows, be sure to choose the option to add Python to your PATH to run it from the command line.

How to run Python code?

You can run Python code in a number of ways, including:

  • The Python interpreter: The Python interpreter is a command-line tool that allows you to type in and run Python code one line at a time.
  • The IDLE editor: The IDLE editor is a simple text editor that comes with Python and allows you to write, run, and debug Python code.
  • An Integrated Development Environment (IDE): An IDE is a more advanced text editor that provides additional features such as code highlighting, debugging, and code completion. Some popular Python IDEs include PyCharm, Visual Studio Code, and Jupyter Notebook.

Understanding Variables

The first concept we’ll cover is variables. Variables are containers that store data. In Python, you can store any type of data in a variable, such as numbers, strings, and even lists.

To create a variable, you use the assignment operator (=) and give the variable a name. For example, if you wanted to create a variable that stores your name, you would write:

name = "John Doe"

In this example, the variable name is assigned the value “John Doe”.

Understanding Data Types

Python has several built-in data types that you can use in your programs. The most commonly used data types in Python are:

  • int (integer): a whole number (e.g. 1, 2, 3, etc.)
  • float (floating point): a number with a decimal point (e.g. 1.0, 2.5, 3.14, etc.)
  • str (string): a sequence of characters (e.g. “Hello, world!”, “John Doe”, etc.)
  • list: a collection of values that can be of any data type (e.g. [1, 2, 3, 4], [“Hello”, “world”], etc.)

You can determine the type of a variable by using the type function. For example:

name = "John Doe"
print(type(name))

This will output <class 'str'>, indicating that the variable name is of type str.

Understanding Operators

There are several types of operators in Python, including:

  1. Arithmetic operators: +, -, *, /, %, ** (exponentiation), // (floor division)
  2. Comparison operators: ==, !=, >, <, >=, <=
  3. Logical operators: and, or, not
  4. Assignment operators: =, +=, -=, *=, /=, %=, **=, //=
  5. Membership operators: in, not in
  6. Identity operators: is, is not

For example, the + operator is used to add two numbers, the == operator is used to check if two values are equal, and the and operator is used to check if both of the two conditions are true.

It’s important to note that operators have different precedence levels, meaning that some operators are executed before others. Parentheses can be used to change the order of precedence.

For example:

x = 5
y = 3

Arithmetic operators:

# Arithmetic operators
print(x + y) # 8
print(x - y) # 2
print(x * y) # 15
print(x / y) # 1.666666...
print(x % y) # 2
print(x ** y) # 125
print(x // y) # 1

Comparison operators:

# Comparison operators
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False

Logical operators:

# Logical operators
print(x > 0 and y > 0) # True
print(x > 0 or y > 0) # True
print(not(x > 0)) # False

Assignment operators:

# Assignment operators
x += y # x = x + y
print(x) # 8
x -= y # x = x - y
print(x) # 5
x *= y # x = x * y
print(x) # 15
x /= y # x = x / y
print(x) # 5.0
x %= y # x = x % y
print(x) # 2.0
x **= y # x = x ** y
print(x) # 8.0
x //= y # x = x // y
print(x) # 2.0

Membership operators:

# Membership operators
numbers = [1, 2, 3, 4, 5]
print(x in numbers) # True
print(x not in numbers) # False

Identity operators:

# Identity operators
y = x
print(x is y) # True
print(x is not y) # False

Note that the above examples are just a few examples of the usage of operators. There are many more ways to use them in your code.

Understanding Conditional Statements

Conditional statements are used to make decisions in your programs based on the values of variables. The most commonly used conditional statement in Python is the if statement.

For example:

age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

This will output You are an adult., since the value of age is greater than or equal to 18.

Tuples

A tuple in Python is an ordered collection of items, similar to a list. However, unlike lists, tuples are immutable, which means that the items inside a tuple cannot be modified after it is created. Tuples are enclosed in parentheses () and the items inside a tuple are separated by commas.

Here’s an example of a tuple in Python:

coordinates = (4, 5)

In the example above, coordinates is a tuple containing two integer items.

Tuples are commonly used to store related data, such as a person’s name and age:

person = ("John", 30)

You can access the items in a tuple using indexing, just like a list:

print(person[0]) # prints "John"

You can also use the built-in function len() to find the number of items in a tuple and the built-in function tuple() to convert a list to a tuple.

Tuples are more memory efficient than lists and are generally used when the data inside it is not intended to be modified. They are also used as a key in a dictionary since dictionary keys need to be immutable.

In summary, tuples in Python are similar to lists, but they are immutable and are used to store a collection of related data; they are also more memory efficient and are commonly used as keys in a dictionary.

https://www.youtube.com/@CodeWithMuh/videos

If you enjoyed my article, show your appreciation with a round of applause! 👏 Your support means the world to me!

Feel free to connect with me across various social media channels! Join me on LinkedIn, YouTube, Twitter, GitHub, and Upwork. Your support means a lot. Thanks for taking the time to read this!

--

--