Python 101: The Ultimate Python Tutorial For Beginners

#python

ยท

5 min read

Let's start from the very beginning. What is Python? Python is a:

"high-level programming language, and its core design philosophy is all about code readability and a syntax that allows programmers to express concepts in a few lines of code" - Guido van Rossum(creator of Python)

For me, the reason I'm learning Python is mostly that it's very readable and its syntax closely looks like the natural language.

Coding in Python can be used in multiple ways: machine learning, data science, and web development. Companies like Intel, IBM, Facebook, JP Morgan Chase, and a number of other massive companies use python for development in different areas. Let's dive right in.

via GIPHY

Installation

The latest version of Python for your operating system can be downloaded here. For setting up your Python development environment using Visual Studio Code here

The Basics

1. Variables

Like any programming language, we have what we call variables, you can think of them as a box that contains something. A variable has a name and contains a value. Think of a box labeled as name and put a name ("Joe") inside it. In programming, name is the variable name, and Joe is the value

In Python, it can be done this way:

name = "Joe"

Pretty easy right? You just assigned the value "Joe" to the variable "name".

another_name = "Jane"

You can assign any other value to whatever other variables you want. Like the second example above, the variable another_name stores the value "Jane".

2. Comments

Comments are readable explanations written in text in the source code of a computer program. They are written with the main purpose of making the source code easier for humans to understand. There are two ways of making a comment.

  • Single line comment, as the name implies can be done on a single line. You use the # infront of your comment.
    # This is a single-line comment.
    
  • Multi-line comment, this can be added to multiple lines.
    '''
    This is a multi-line comment
    using docstring
    '''
    

3. Control Flow: conditional statements

A program's control flow is the order in which the program's code executes. The control flow of a Python program is regulated by conditional statements, loops, and function calls. But we would only cover the if, else, and elif statement.

"If" uses an expression to check whether a statement is True or False. If it is True, it executes what's inside the "if" statement. For example:

if True:
      print("Hello World") # This prints Hello World

if 2 > 1:
      print("2 is greater than 1") # 2 is greater than 1, so the code is executed

The "else" statement is executed if the "if" expression is false.

if 1 < 2:
      print("1 is greater than 2")
else:
      print("1 is not greater than 2")

1 is not greater than 2, so the code inside the "else" statement is executed.

The "elif" statement can also be used this way:

if 1 > 2:
  print("1 is greater than 2")
elif 2 > 1:
  print("1 is not greater than 2")
else:
  print("1 is equal to 2")

4. Looping/Iterator

In Python, we can iterate in different ways. The basic two are while and for.

While Looping: while the statement is True, the code inside the block will be executed. So, this code will print the number from 1 to 10.

num = 1

while num <= 10:
    print(num)
    num += 1

The while loop needs a โ€œloop condition.โ€ If it stays True, it keeps iterating. when num is 11 in this example the loop condition equals False.

For Looping: used to repeatedly execute a group of statements as long as the condition is satisfied.

for i in range(1, 11):
  print(i)

This code will keep printing until the 11th element

Data Types

Now let's talk about Data Types. What are they? In simple terms, they are how you store your data. In Python, every value has its own data type which we'd look at now. These are six data types in Python.

  • Strings
  • Numbers
  • Booleans
  • Lists
  • Tuples
  • Dictionaries

1. Strings

Strings are created simply by enclosing characters in quotes "".

language = "Python"

2. Numbers

Python supports three numeric data types: int, float, and complex numbers. They are:

age = 26 # int
pi = 3.11 # float

3. Booleans

Booleans represents one of two values: True or False

a = True
b = False
if a is True and b is False:
  print("Hello World!")

4. Lists

Lists in python are used to store multiple items in a single variable. In other programming languages, what we call an array. They are nested in square brackets [].

favourite_colors= ["blue", "orange", "white"]

5. Tuples

Tuples like lists are used to store multiple items in a single variable but are unchangeable. They are written with round brackets ().

this_tuple = ("apple", "banana", "cherry")

6. Dictionaries

Dictionaries are used to store data values in key: value pairs. It is a collection that is ordered, changeable, and does not allow duplicates. Dictionaries are written with curly brackets and have keys and values.

this_dict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

anddd The End!

We learned a lot of things about Python basics, but this is just the surface. There is so much more other cool stuff so keep learning, have fun and always keep coding. ๐Ÿ˜๐Ÿ‘‹

You can find me: My Twitter & Github

via GIPHY

ย