DEV Community

Cover image for Python 101: The Ultimate Python Tutorial For Beginners
BochaberiDaisy
BochaberiDaisy

Posted on

Python 101: The Ultimate Python Tutorial For Beginners

Python is a popular and fast growing programming language. It was invented by Guido Van Rossum, a Dutch programmer. Programmers fall in love with Python as it increases productivity. Simple and understandable syntax and easy debugging are some of the features of Python that make the language loveable. The main differentiator of Python from other language is the rule on indentation, as we'll come to learn later on here, it is a big deal.
It is used in some of the popular web products such as Youtube and Google.

Features of Python

  1. It being a high level language makes it is easy to code thus a good beginner-friendly language learn.
  2. It is an open source language making its source code available to the public.
  3. It is versatile supporting both functional and object oriented programming.
  4. It is portable. Python code can be run on different operating systems without the need of changing the code.
  5. It is easily integrated with other languages such as C, C++, etc.
  6. It is an interpreted language. Python code is executed line by line at a time. It also doesn't require to be compiled like many other languages

Applications of Python

  • Web programming

  • Scripting

  • Scientific computing

  • Embedded development

Installation

There are various versions of Python. The most common are Python 3 and Python 2. Python 3 is a newer and better version of Python 2. If you try to run the Python 2 code in Python 3, then you will get the error and vice versa. This is indicates that the syntaxes are different.
Click here to download Python.

Comments

Comments help other readers and your future self understand your code better. It also improves code readability.

# This is a comment

""" This is 
a multiline 
comment
"""

#Comments are only readable to humans.

Enter fullscreen mode Exit fullscreen mode

Print

print is a built-in function which displays the logs of your code. The text should be enclosed in single or double quotes
Here comes the first line of code every programmer outputs when they learn a new language, 'Hello World'.

print('Hello World')
print("Hello World")

#This will cause a SyntaxError
print('Hello World")
Enter fullscreen mode Exit fullscreen mode

Indentation

Indentation are the leading whitespaces i.e. spaces and tabs before any statement in python. As it is in other languages such as C# and C++, indentation serves the purpose of code readability. In Python, indentation is mandatory as statements with the same level of indentation are treated as a single code block. It much like how curly braces{} work in other languages such as C and Java.

To indent you can use the Tab key or use 4 spaces.

Forget to indent a statement and the Python interpreter will throw a IndentationError. It might seem like a constant bug when you get the errors but once you get used to the concept you'll have gained a good programming practice applicable in other languages as well.

Variables

Variables are the names assigned to a value and are used to refer to the value later in the program. The value assigned to a variable is called a literal.
Initializing a variable in Python doesn't require you to assign the type to the variable as it is in other languages. Assigning a value to a variable is done with the equal = sign.

There are rules that apply in regards to the characters used in Python variable names. They include;

  • Only letters, numbers and underscores are allowed.

  • They can't start with numbers.

  • Keywords such as global can't be used.

user = "Monique"
age = 28

#This results in an error
123name = "Mark"
name@2 = "Marilyn"

Enter fullscreen mode Exit fullscreen mode

Data types

Data types are the classification of data items. Python has a built-in function type that is used to check for the data type.
Some of the basic data types in Python include:

1. Integers

These are whole numbers that can be positive, negative or zero.

num = 83653
temp = -40

type(num)
type(temp)

Enter fullscreen mode Exit fullscreen mode

Output:

<class 'int'>
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

2. Complex numbers

These specifies real part + (imaginary part) j.

a = 22 + 7j
type(a)
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'complex'>
Enter fullscreen mode Exit fullscreen mode

3. Float

This is a real number with a floating point representation. It is specified with the decimal point.
Divisions or operations done with a float result into floats.

height = 124.34
type(height)
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'float'>
Enter fullscreen mode Exit fullscreen mode

4. String

Strings are sequences of character data. String literals are delimited using either single or double quotes.

type("I am a string.")
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Escape sequences in strings are used to interpret a character or sequence of characters within a string differently. This is accomplished by using a backslash () character.

print("It\'s fun learning Python.")
Enter fullscreen mode Exit fullscreen mode

Output:

It's fun learning Python.
Enter fullscreen mode Exit fullscreen mode

5. Boolean

This has two built-in values, True or False.

type(True)
type(False)
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'bool'>
<class 'bool'>
Enter fullscreen mode Exit fullscreen mode

6. Lists

Ever heard of an array or a matrix, lists are pretty much the same except it is more efficient. It is created by placing elements inside square brackets [], separated by commas. It can contain items of different types (strings, integers, etc). It can also have another list which is called a nested list.

list = ["father", 34, "mother", 32, "brother", 10]
print(list)
Enter fullscreen mode Exit fullscreen mode

Output:

["father", 34, "mother", 32, "brother", 10]
Enter fullscreen mode Exit fullscreen mode

To access items on a list we can use the index operator []. Indices start from [0] and [-1] indicates the end of the list. Negative indices start from the end of the list to the left.

list = ["father", 34, "mother", 32, "brother", 10]
print(list[-3])
print(list[0])
Enter fullscreen mode Exit fullscreen mode

Output:

32
father
Enter fullscreen mode Exit fullscreen mode

To change a list i.e. to add to it or remove from it, we can use built-in functions.
append() - adds items to end of the list.
pop() - removes items from the end of the list.

list = ["father", 34, "mother", 32, "brother", 10]
list.append("sister")
print(list)
list.append(6)
print(list)
list.append(867)
print(list)
list.pop()
print(list)
Enter fullscreen mode Exit fullscreen mode

Output:

['father', 34, 'mother', 32, 'brother', 10, 'sister']
['father', 34, 'mother', 32, 'brother', 10, 'sister', 6]
['father', 34, 'mother', 32, 'brother', 10, 'sister', 6, 867]
['father', 34, 'mother', 32, 'brother', 10, 'sister', 6]
Enter fullscreen mode Exit fullscreen mode

Operators

1. Arithmetic operators

This includes; addition, subtraction, multiplication, division, exponentiation, modulus, floor division
Exponentiation is the raising of a number to the power of another number. The operation is performed by using two asterisks **.
Modulus is used to get the remainder of a division and is performed using the percent symbol %
Floor division is used to determine quotient of a division. The operation is done using two forward slashes //

a = 4
b = 3

print(a + b) #Addition
print(a - b) #Subtraction
print(a * b) #Multiplication
print(a / b) #Division
print(a ** b) #Exponentiation
print(a % b) #Modulus
print(a // b) #floor division
Enter fullscreen mode Exit fullscreen mode

Output:

7
1
12
1.3333333333333333
64
1
1
Enter fullscreen mode Exit fullscreen mode

2. Comparison operators

These include equal to ==, greater than >, greater or equal to >=, less than <, less or equal to <=

a = 4
b = 3
# Greater than
 a > b 
# less than
a < b 
# Equal to
a == b 
# Greater than or equal to
a >= 2 
# Less than or equal to
b <= 1 

Enter fullscreen mode Exit fullscreen mode

Output:

True
False
False
True
False
Enter fullscreen mode Exit fullscreen mode

3. Logical operators

These are conjunctions used to combine more than one condition. They include not, and, or

a = 3
b = 7
#and
print(a == 4 and b == 7)
#or
print(a == 4 or b == 7)
#not
print(not(a == 4))

Enter fullscreen mode Exit fullscreen mode

Output:

False
True
True
Enter fullscreen mode Exit fullscreen mode

4. Assignment operator

These are used to assign values to variables. They include =, +=, -=, *=, /=, //=, %=, **=
For the assignment operators excluding =, the operation on the left is done on the values before they are assigned to the variables.

a = 7
print(a)
a += 2
print(a)
a -=3
print(a)
a *=2
print(a)
a /= 3
print(a)
a //= 3
print(a)

Enter fullscreen mode Exit fullscreen mode

Output:

7
9
6
12
4
1
Enter fullscreen mode Exit fullscreen mode

5. Membership operator

They are used to test whether a value is a member of a sequence i.e. a list, a string or a tuple.
The operators include; in which checks if a value is a member of a sequence and not in which checks if a value is not a member of a sequence.

fruits = ['guava', 'banana', 'tomato', 'apple']
print('guava' in fruits)
print('kales' not in fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

True
True
Enter fullscreen mode Exit fullscreen mode

6. Identity operator

They test whether two operands share an identity. They include is which tests if they share an identity and is not which tests if they don't share an identity.

print(2 is not '2')
print('4' is not "4"

Enter fullscreen mode Exit fullscreen mode

Output:

False
False
Enter fullscreen mode Exit fullscreen mode

Loops

Loops are used to iterate over a given sequence i.e. lists, tuples or other iterable objects.

if

if iterates over a sequence when a certain logic condition is met.

count = 0
while count <= 5:
    print(count)
    count += 1 
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

for

for loops can iterate over a sequence of numbers using the range() function. range() returns a new list with numbers of that specified range.

for x in range(5):
    print(x)
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
Enter fullscreen mode Exit fullscreen mode

while

while loops repeat as long as a certain boolean condition is met.

count = 0
while count < 5:
    print(count)
    count += 1 
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
Enter fullscreen mode Exit fullscreen mode

else / elif

else can be used when a loop fails then the code block else is executed. elif , however, is the short of else...if.

age = 16
if age < 1 and age >= 0:
    print("Infant")
else if age >= 13 and age < 21:
    print("Teen")
elif age >= 21:
    print("Adult")
Enter fullscreen mode Exit fullscreen mode

Output:

Teen
Enter fullscreen mode Exit fullscreen mode

break and continue

break is used to exit a for loop or a while loop.
continue is used to skip the current block, and return to the for or while statement.

count = 0
while True:
    print(count)
    count += 1
    if count = 2:
        continue
    if count >= 5:
        break
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
3
4
Enter fullscreen mode Exit fullscreen mode

Functions

These are groups of related statements that perform a specific task. These help keep things organized and manageable when the program gets larger. They also help avoid repetition and makes the code reusable.

Syntax of a function

def function_name(parameters):
    statement(s)
    return statement

Enter fullscreen mode Exit fullscreen mode

def is a keyword that marks the start of a function header.
Next comes the function_name which is used to uniquely identify your function. Naming your function follows the same rules used to name variables.
Parameters are the arguments through which values are passed to aa function.
The colon : marks the end of a function header.
statement(s) is where your code goes in.
An optional return statement can be added to make your function return a specific value.

Example of a function and how to call it

def welcome_message(name):
"""
    This function welcomes 
    a user to Python
    """
    print ("Hello" + name + ". Welcome to Python!!")


#callng the function
welcome_message(Frank)

Enter fullscreen mode Exit fullscreen mode

Output:

Hello Frank. Welcome to Python!!
Enter fullscreen mode Exit fullscreen mode

Note: Notice how we indent the code that is within our function and not the when we call our function.

Other than creating our own functions which are called user-defined functions, Python has built-in functions that are readily available for use. We have already mentioned a few such as print(), input(), type(), range(), etc. Below are others, just to mention a few.

abs() - returns the absolute value of the specified number.

negative_number  = -22
print(abs(negative_number))
Enter fullscreen mode Exit fullscreen mode

Output:

22
Enter fullscreen mode Exit fullscreen mode

sorted() - returns a sorted list of the specified iterable object such as tuples.

tuple = ("h", "b", "a", "c", "f", "d", "e", "g")
print(sorted(tuple))
Enter fullscreen mode Exit fullscreen mode

Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Enter fullscreen mode Exit fullscreen mode

len() - returns the number of items (length) in an object.

pets = ['Miles', 'Trevor', 'Kathy']
print(len(pets)
Enter fullscreen mode Exit fullscreen mode

Output:

3
Enter fullscreen mode Exit fullscreen mode

help()- displays the documentation of modules, functions, classes, keywords. It works for both user-defined and built-in functions. If you are stuck, this function is just a call away!!

There are more concepts in Python such as Stacks, Queues and Object Oriented Programming which require these concepts as a basis. Go practice code and learn some more in whatever sequence!

Top comments (0)