DEV Community

Chris Mwalimo
Chris Mwalimo

Posted on

Python 101: The Ultimate Python Tutorial for Beginners

Python is a high-level language that was designed by Guido Van Rossum and released in 1991.

Over the years, Python has grown to be popular and tops many charts as it gains more users. It is termed as a Beginner-friendly language because of its simple syntax and how easy it could be to master it.

Python has become one of the most reliable programming languages in different fields of specialization. From Data Science, Machine Learning, Artificial Intelligence to software engineering; Python is one of the best languages for a beginner who aims to learn programming or add something cool to their portfolio.

What are some of the features of Python?

  • Object-Oriented Language: Your python program consists of set of objects that you used to solve a problem.
  • Easy-to-learn language: Python is a language that is easy to learn and doesn't put pressure on a beginner desiring to learn programming.
  • Interpreted language: Python is an interpreted language. It won't require you to manually compile and execute your program; just write code and run.
  • Free and Open Source language: Python is an open-source language that is free for everyone to learn and use.
  • Huge Standard Library: Python has you covered with a cool standard library that supports various specialization fields like web dev and data science.

This are just some of the features of Python programming language.

How to install and set-up Python:

You can download the latest version of Python for your PC or laptop from here. Versions for Windows, Linux and MacOS are available.

You can go through a guide on installation of Python on various OS platforms from here and also know how to configure Python for VSCode from here.

Note: There are various websites you can use to learn Python. For starters, I would recommend Programiz and GeeksforGeeks for learning the basics of Python and some advanced topics.

Commonly used IDEs for Python

There are two commonly-used IDEs that are beginner-friendly. They are:
*VSCode
*PyCharm Community Edition

Writing your first Python program: Hello World!

This first program is very easy and shows a portion of what will be dealt with. Check out the code:

print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

The code consists of print() which is a built-in function used to print expressions(strings, numbers, etc.) written within parentheses and "Hello World!" string.
The code output after running the program is:

Hello World!
Enter fullscreen mode Exit fullscreen mode

Comments

Comments are used as to describe what happens at some point in the code of a program or to leave a message for another developer modifying the code. It's very meaningful in a code-space. Python comments have their own syntax:

#This is a single-line comment

"""This is a
multi-line
comment, usually referred to as Docstring"""
Enter fullscreen mode Exit fullscreen mode

Variables

Declaring variables in Python isn't as complicated or very precise like in C++ where you specify the data-type before declaring your variable.

name = "John Doe"
age = 21
Enter fullscreen mode Exit fullscreen mode

As displayed in the code block above, you declare a variable by simply declaring a variable name and add a value to it and you're set to proceed.
The output:

John Doe
21
Enter fullscreen mode Exit fullscreen mode

When writing strings, you could use single quotation marks(' ') or double quotation marks(" "). Both work perfectly.

Taking input from user

Take for example, you want to create a program that takes a user's name and age and returns a statement welcoming them. You will need to take input from the user interacting with the program. You can do that with ease as shown below:

name = input("Enter your name: ")
age = int(input("Enter your age: "))

print("Welcome,", name, ". You are", age, "years old!" )
Enter fullscreen mode Exit fullscreen mode

We declare the variable 'name' and request the user to enter their name. The same is done for 'age' but the data-type is specified because the input() function takes strings by default.
After the user inputs their name and age as 'James Doe', 30, the output becomes:

Welcome, James Doe . You are 30 years old!
Enter fullscreen mode Exit fullscreen mode
Concatenation of strings with variables

Concatenating of strings with variables can be done in various ways. The variables used in Taking input from user shall be used to explain concatenation

  1. Use of the addition (+) operator:
"""While using addition operator to concatenate, convert 
all int variables to string with str() function"""

print("Welcome, " + name + ". You are " + str(age) + " years old!")
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome, James Doe. You are 30 years old!
Enter fullscreen mode Exit fullscreen mode
  1. Use of comma(,)
print("Welcome,", name, ". You are", age, "years old!")
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome, James Doe . You are 30 years old!
Enter fullscreen mode Exit fullscreen mode

Python Operators

Python operators serve different purposes as listed below:
Arithmetic: These operators are used to perform arithmetic operations in Python. They include: +, -, //, %, **, *, /

print(5 + 2) #addition
print(5*7) #multiplication
print(2**5) #exponential
print(18/2) #division
print(3%4) #modulus
print(25//5) #floor division
print(5 - 4) #subtraction
Enter fullscreen mode Exit fullscreen mode

Output:

7
35
32
9.0
3
5
1
Enter fullscreen mode Exit fullscreen mode

Comparison: These operators are used to compare two things and return a boolean result. They include: >, <, <=, >=, ==, !=

x = 20
y = 10

print(x < y) #less than
print(x > y) #greater than
print(x <= y) #less than or equals to
print(x >= y) #greater than or equals to
print(x != y) #not equal to
print(x == y) #equals
Enter fullscreen mode Exit fullscreen mode

Output:

False
True
False
True
True
False
Enter fullscreen mode Exit fullscreen mode

Logical: These operators are used to combine conditional statements. They include: and, or, not

x = 20
y = 10

print(x < 30 and y > 9)
print(x > 10 or y < 5)
print(not(x < 5 and y < 10))
Enter fullscreen mode Exit fullscreen mode

Output:

True
True
True
Enter fullscreen mode Exit fullscreen mode

Identity and membership: Identity operators return true if a both values are the same and false if otherwise. They include: is, is not. Membership operators return true is an object is present in another and false if otherwise. They include: in, not in.

x = 20
y = 10

#identity
print(x is y)
print(x is not y)

#membership
print("ment" in "mentality")
print("mint" not in "mentality")
Enter fullscreen mode Exit fullscreen mode

Output:

False
True
True
True
Enter fullscreen mode Exit fullscreen mode

Python statements and loops

If...else and if...elif statements
You want to create a Python program that does one thing if input is true and does another if input is false; that is where you implement the if...else and if...elif statements.

  • If...else statement:
age = int(input("How old are you: "))

if age < 13:
    print("You are not allowed to play this game!")
else:
    print("You can proceed to the arena.")
Enter fullscreen mode Exit fullscreen mode

Output:
Output depends with the input given by the user. If age
is greater than 13, you shall proceed but if otherwise, you will not be allowed to play the game.

  • If...elif statement:
marks = int(input("Enter your score: "))

if marks >= 70:
    print("Pass")
elif marks >= 50:
    print("Average")
else:
    print("Good try! You can do better next time!")
Enter fullscreen mode Exit fullscreen mode

Output:
If marks are 70 and above, output will be "Pass. If they are from 50 to 69, output will be "Average while if 49 and lower, output becomes "Good try!..."

Python Loops

  1. For Loop

For loop in Python is used for iterating over a sequence.

students = {"Zelda", "John", "Jane", "Simon"}
for student in students:
    print(student)
Enter fullscreen mode Exit fullscreen mode

Output:

Simon
John
Zelda
Jane
Enter fullscreen mode Exit fullscreen mode
  1. While Loop

While loop executes a set of statements as long as the condition is true or satisfied. While loops can execute to infinity so it is important to use increments or break statement.

n = 10
count = 0
while count < n:
    count += 1 #increment added so loop can break after reaching n
    print(count)
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
6
7
8
9
10
Enter fullscreen mode Exit fullscreen mode

Python Data structures

  • Lists

Lists, commonly known as arrays, are data structures with changeable sequences of elements. Elements inside a list are called items. They are defined by square brackets - [ ].

market = ["books", "groceries", "bread", "electronics"]
print(market)
Enter fullscreen mode Exit fullscreen mode

Output:

['books', 'groceries', 'bread', 'electronics']
Enter fullscreen mode Exit fullscreen mode
  • Tuples Tuple is a data structure that stores an ordered sequence of elements. They cannot be changed once or modified like lists. They are described by parentheses - ( ).
market = ("books", "groceries", "bread", "electronics")
print(market)
Enter fullscreen mode Exit fullscreen mode

Output:

("books", "groceries", "bread", "electronics")
Enter fullscreen mode Exit fullscreen mode
  • Sets Set is a data structure used to store multiple elements in a single variable. They are unindexed, unordered and cannot be changed. They are described by curly brackets - {}.
students = {"Zelda", "John", "Jane", "Simon"}
print(students)
Enter fullscreen mode Exit fullscreen mode

Output:

{"Zelda", "John", "Jane", "Simon"}
#note: order of elements might change as sets are unordered.
Enter fullscreen mode Exit fullscreen mode
  • Dictionaries Dictionary is a data structure that stores elements in a key:value pairs. They are also known as associative arrays and described by curly brackets - {}.
students = {1: "Zelda", 2: "John", 3: "Jane", 4: "Simon"}
print(students)
Enter fullscreen mode Exit fullscreen mode

Output:

{1: "Zelda", 2: "John", 3: "Jane", 4: "Simon"}
Enter fullscreen mode Exit fullscreen mode

Python user-defined functions

A python user can declare functions using the def keyword and thereafter, give their function a name, include arguments if any present inside parentheses and define what the function would do.
Even without arguments, always include parentheses for environment to identify that a function is being declared.

#declaring the function
def powers(y, x):
    n = y**x
    return n

#call the function and give arguments to get output 
powers(2, 2)
Enter fullscreen mode Exit fullscreen mode

Output:

4
Enter fullscreen mode Exit fullscreen mode

Python Classes

Python is an object-oriented programming language and thus, the introduction of classes and objects.

  • self parameter is used to access variables used in a class
  • init() function is used to assign values to object properties.
class person:         #declaring a class that takes user name and age
    def __init__(self, name, age): #initializing properties
        self.age = age
        self.name = name

p = person("John", 21) #declaring an object

print(p.name, p.age)
Enter fullscreen mode Exit fullscreen mode

Output:

John 21
Enter fullscreen mode Exit fullscreen mode

Conclusion:

This is just the beginning. Python is a very interesting programming language that you can use to develop your skills in programming and become a pioneer in your desired field of work. Happy Coding and all the best as you begin your Python journey!

Top comments (2)

Collapse
 
roy_maingi profile image
Roy Maingi

Easy to understand and very practical.

Collapse
 
codeschris profile image
Chris Mwalimo

Thank you very much!