Python for Beginners (Part 2): Variables and Operators

Variables and operators are an essential part of any programming language and in this part you will expand your programming skills in Python In the previous part of this tutorial series, Python for Beginners (Part 1): Getting Started, we started our journey in Python. Do you want to learn more about Python? Great! In this part we will expand our programming skills by becoming familiar with Python variables and operators.

Python Commenting Basics

In Python, single line comments are made using the hash (#) character. Comments are needed to:

  • make the code readable,
  • explain the code,
  • avoid execution while testing the code.
    #This is a comment
    print('Hello World')
    print('Hello World') #This is a comment.
    #print ('Hello World')
    print ('My name is Funmi')

Python Multiline Comments

Multiline comments can be assigned using multiline strings with triple quotes (“””) in your code.

my name is Funmi
and I'm a programmer"""
print('Hello World')

Variables in Python

A variable is a container for a value that can be of different types. It allows you to refer to a value with a name.

Guidelines for Assigning Variables

  • Variable names are case sensitive, e.g., ‘Hello’, ‘HELLO’, and ‘hello’ are three different variables.
  • A variable can either begin with a letter or an underscore but not a number and they can’t have spaces between them.
  • To assign a value to a variable, use the equal sign (=), e.g., x = 5, pi = 3.14.
  • The best way to name variables in Python is to use all lowercase letters and underscores to separate words, e.g., my_name = ‘John’.

Variable Types

  • Integer (int): This is a number without a fraction or decimal, e.g., 2, 1000, 50, etc.
  • Floating point (float): Is a number that has both integer and fractional part, e.g., 2.2, 1.0, 100.0 etc.
  • String (str): It represents a text. You use single quotes (‘ ‘) or double quotes (” “) to build a string. You can use backslash (\) to include special characters such as ‘ ‘ ‘.
  • Boolean (bool): It represents logical values which can only be True or False.

If you are not sure of the type of variable in a particular code block, use the type() function as shown below:

x = 'Hello World'
print (type(x))

You can also convert variable types from one form to another. For example, you can convert an int variable to a float variable and vice-versa.

#Conversions to int
x = int(2)
y = int(2.8)
z = int('2')

#Conversions to float
x = float(2)
y = float(2.8)
z = float('2')

#Conversions to string
x = str(2)
y = str(2.8)
z = str('2')

Operators in Python

Arithmetic Operators

The following are arithmetic operators used in Python:

  • + Addition
  • Subtraction
  • * Multiplication
  • / Division
  • ** Exponentiation
  • % Modulo – This returns the remainder after dividing the first number by the second number.
  • // Integer Division – This divides one integer by another but rather than giving the exact answer, it rounds down the answer to an integer.

Comparison Operators

These are used to compare the values of variables. They always return a boolean value (True or False). Listed below are comparison operators used in Python:

  • < Less than
  • > Greater than
  • <= Less than or equal to
  • >= Greater than or equal to
  • == Equal to
  • != Not equal to
7 != 1
5 < 8
2 == 3

Logical Operators

Logical operators are used to combine conditional statements. The following are logical operators in Python and their descriptions:

  • and – returns True if both statements are True
  • or – returns True if at least one statement is True
  • not – executes the statement in the parenthesis, then inverts or negates the output
6==4 and 6 > 4
6!=4 and 6 > 4
6==4 or 6 > 4
not(6==4 or 6 != 4)

Identity Operators

These operators are used to compare objects if they are actually the same object (not if they are equal).

  • is – returns True if both variables are the same objects
  • is not – returns True if both variables are not the same objects
x = ['orange', 'apple']
y = ['orange', 'apple']
z = x

print(x is z)

# returns True because z is the same object as x

print(x is y)

# returns False because x is not the same object as y, even if they have the same content

print(x == y)

# to demonstrate the difference between "is" and "==": this comparison returns True because x is equal to y


Your journey in Python has started on an excellent note. With basic knowledge on variable types and operators, next you will learn how to piece together these building blocks to write complex codes.


Related Posts