A programming language is a language specifically designed to express computations that can be performed by a computer.

Programming languages are used to create programs that control the behavior of a system, to express algorithms, or as a mode of human communications. Python, C, C++, and Java have wide use.

Python is a high-level programming language which was developed by Guido Van Rossum in 1991. Due to its easy readability and multi-purpose nature, it makes it better than other languages.

In comparison with C or C++, Python is very well suited for beginners. It has few keywords, a well-defined syntax, can run on a wide variety of hardware platforms, and has the same interface on all platforms.

Python is an Object-Oriented Programming language that can encapsulate code within objects.

The major topics that are learned in Python are indentation, data types, comments, operators, decision making, loops, functions, modules, keywords, OOPs, libraries, and many more.

Today we will discuss the first five.

1. Indentation

In a Python program, the leading white space at the beginning of the logical line determines the indentation level.

"All statements inside the block should be at the same indentation level".

Python checks the indentation level very strictly and gives an error if the indentation is not correct.

In most programming languages, indentation has no effect on program logic. It is used to align statements to make the code readable. However, in Python indentation is used to associate and group statements.

As an example, we are making a function a() and in it you can see two comments with some space initially and a pass statement with space initially. That “initial space” is called indentation and these three lines are the block of code that is inside the function.

def a(param1, param2):
    
    # code line 1 with indentation
    # code line 2 with indentation
    pass

2. Data Types

Python is a dynamically typed language; hence we do not need to define the type of a variable while declaring it.

"The interpreter implicitly binds the value with its type.""

Data types are the classification or categorization of data items. It represents the kind of value that determines what operations can be performed on a particular data.

There are 5 data types in python: numeric, sequence type, boolean, set, and dictionary.

Numeric: They are defined as int, float, and complex classes in Python.

  • int: It consists of all integer numbers. Integers can be either positive or negative. Built in size of integers is unlimited. Examples: -5, 0, 890.
  • float: Floating point numbers are written as decimal numbers separated by a decimal point. They are expressed in the form of an integer number and fractional part separated by a decimal point. Examples: -5.2, 11.325.
  • complex: Complex numbers are expressed in the form of a+bi, where a is the real part and b is the imaginary part. Example: 4+5i.

Sequence type:  A sequence is an ordered collection of similar or different data types. The three main types of sequences are string, tuple, and list.

  • string: A string is a sequence of Unicode characters.  We can use single quotes, double quotes, or triple quotes to represent strings.  Multi-line strings can be denoted using triple quotes,('''...''') and single-line strings can be denoted using single quotes('...') or double quotes("...").
  • tuple: A tuple is an ordered collection of values. Tuples are immutable, so once created they cannot be modified.  They are usually defined within parentheses, ( ), where items are separated by commas. Tuples can contain any number of elements and of any datatype (like strings, integers, lists, etc.).
  • list: A list is an ordered sequence of items, similar to tuples.  They also represent a collection of values, which can be of different data types. Items in a list are separated by commas and are enclosed within brackets, [ ]. Lists are mutable while tuples are immutable.

Boolean: Boolean is a data type with one of the two built-in values, True or False. True and False with a capital ‘T’ and ‘F’ are valid booleans.

Set: It is an unordered collection of values that is iterable, mutable, and has no duplicate elements.

Dictionary: A dictionary is an unordered collection of items, structured as key: value pairs. In the dictionary, each key-value pair is separated by commas inside the curly braces, { }.  In a dictionary, each key should be unique.

Code example:

x = "Hello, world!"  # string
x = 20  # int
x = 20.5  # float
x = 1j  # complex
x = ["values", "separated", "by", "commas"]  # list
x = ("also", "separated", "here")  # tuple
x = {1, 2, 3}  # set
x = {"name": "Bob", "age": 35}  # dictionary
x = True  # boolean

3. Comments

The comments are used to add additional content information about the program or its statements. There are single-line comments and multi-line comments.

Single-line comments start with a hash character (#) and are followed by text that contains further explanations.

Code example:

x = "The line below is a single-line comment."

# This is a single-line comment in Python!

Multi-line comment blocks are also understood by Python. These comments serve as in-line documentation for others reading your code and explaining things in more detail.

Code example:

"""
This is a Python code example.

This is a multi-line comment that spans 5 lines.
"""

4. Operators

Python operators are symbols that are used to perform mathematical or logical manipulations. Operands are the values or variables to which the operator is applied.

There are a few types of operators in Python, let's talk about each one in detail.

4.1. Arithmetic Operators

As the name suggests, these operators are used to perform arithmetic operations.

The code example will show all the arithmetic operators with the respective output for each.

x = 23
y = 5

# Addition
x + y  # 28

# Subtraction
x - y  # 18

# Multiplication
x *  y  # 115

# Division (always returns a float)
x / y  # 4.6

# Floor division
x // y  # 4

# Modulus (remainder of division)
x % y  # 3

# Power
x ** y  # 6436343

4.2. Assignment Operators

Assignment operators are used to assign values to variables.

This code example will show you how all assignment operators work in Python.

x = 23
x += 5  # 28
x -= 5  # back to 23
x *= 2  # 46
x /= 2  # back to 23
x %= 5  # 3 (23 / 5 has a remainder of 3)
x **= 2  # 9

# There are more!

4.3. Comparison Operators

They are used to compare two values.

This code example will show you how each comparison operator works.

x = 5
y = 6
z = 5

x == y  # False
x == z  # True

y > z  # True
x < y  # False

x >= z  # True
x <= z  # True

4.4. Logical Operators

Logical operators are generally used for combining two or more relational statements. Often we use them with boolean values, but they have more uses too.

Code example showing how to use logical operators in Python:

x = 3
y = 4

x > y and x == 3  # False
x < y and x == 3  # True
x > y or x == 3  # True
not x > y  # True

4.5. Identity Operators

They are used to compare the objects, it returns True if they are the same object with the same memory location.

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z)  # True, they are the same list

print(x is y)  # False, same values but different "thing"

4.6. Bitwise Operators

They are used to compare binary numbers. You’ll use these seldom, but it’s good to know they exist.

The code below is taken from here, where you can read an in-depth explanation if you're interested.

a = 10  # 1010
b = 4  # 0100

print(a & b)  # 0

print(a | b)  # 14

print(~a)  # -11

print(a ^ b)  # 14

5. Decision making in Python

We can control the flow of execution of a program using "if statements". We can give the statement a condition, and if the condition is True then the block below it will execute. If the condition is False then the block will not execute.

In this code example:

  • If the value of a is greater than 5 then we are adding 5 to it.
  • Otherwise, if a is equal to 4 then we are adding 3 to it.
  • Finally, if neither of the above executed, we are adding 2 to it.
a = 4

if a > 5:
    a += 5
elif a == 4:
    a += 3
else:
    a += 2

print(a)  # 7

Conclusion

This was all for the first part of "Basics of Python a Beginner Should Know". I have also written a small thread on this topic and that thread is inspired by this article.

If you want to learn more about Python, consider enrolling in our Complete Python Course which takes you from beginner all the way to advanced (including OOP, web development, async development, and much more!). We have a 30-day money-back guarantee, so you really have nothing to lose by giving it a try. We'd love to have you!

Photo by Christopher Gower on Unsplash