Archive May 2020

Python Variables

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Variable also known as identifier and used to hold value.

In Python, we don’t need to specify the type of variable, because Python is language and smart enough to get variable type.

How to define Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

Declaring Variable and Assigning Values

Python allows us to create variable at required time. It does not bound us to declare variable before using in the application. We don’t need to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically.

The equal (=) operator is used to assign value to a variable.

counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = “John” # A string

Here, 100, 1000.0 and “John” are the values assigned to countermiles, and name variables, respectively.

Multiple Assignments

Python allows us to assign a value to multiple variables in a single statement which is also known as multiple assignments. We can apply multiple assignments in two ways either by assigning a single value to multiple variables or assigning multiple values to multiple variables. Lets see given examples.

X = Y = Z = 50

Let’s explore above concept through jupyter notebook

Numbers and more in Python

In this lecture, we will learn about numbers in Python and how to use them.

We’ll learn about the following topics:

1.) Types of Numbers in Python

2.) Basic Arithmetic

3.) Differences between classic division and floor division

4.) Object Assignment in Python

Types of numbers:

There are three numeric types in Python

  • int
  • float
  • complex

Variables of numeric types are created when you assign a value to them:

x = 1    # int
y = 2.8  # float
z = 1j   # complex

Let’s explore about the numbers through jupyter notebook.

Python String Formatting

String formatting lets you inject items into a string rather than trying to chain items together using commas or string concatenation. As a quick comparison, consider:

player = ‘Thomas’
points = 33
‘Last night, ‘+player+’ scored ‘+str(points)+’ points.’ # concatenation
f’Last night, {player} scored {points} points.’ # string formatting

There are three ways to perform string formatting.

  • The oldest method involves placeholders using the modulo % character.
  • An improved technique uses the .format() string method.
  • The newest method, introduced with Python 3.6, uses formatted string literals, called f-strings.

Since you will likely encounter all three versions in someone else’s code, we describe each of them here.

Let’s explore the concept through jupyter notebook.

Python List

List is a collection which is ordered and changeable. It allows duplicate members.

A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].

A list can be defined as follows.

L1 = [“John”, 102, “USA”]
L2 = [1, 2, 3, 4, 5, 6]
L3 = [1, “Ryan”]

Let’s explore more concepts about list through following jupyter notebook exercise.

Python Dictionaries

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.

dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]

When the above code is executed, it produces the following result

dict[‘Name’]: Zara
dict[‘Age’]: 7

Let’s Explore more about dictionaries through jupyter notebook.

Python Sets

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.


thisset = {“apple”, “banana”, “cherry”}

Access Items

You cannot access items in a set by referring to an index, since sets are unordered the items has no index.

But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

Let’s explore Sets and Booleans concepts with Jupyter notebook.

Comparison Operators

In this lecture we will be learning about Comparison Operators in Python. These operators will allow us to compare variables and output a Boolean value (True or False).

If you have any sort of background in Math, these operators should be very straight forward.

First we’ll present a table of the comparison operators and then work through some examples:

Table of Comparison Operators 

In the table below, a=3 and b=4.

==If the values of two operands are equal, then the condition becomes true.(a == b) is not true.
!=If values of two operands are not equal, then condition becomes true.(a != b) is true
>If the value of left operand is greater than the value of right operand, then condition becomes true.(a > b) is not true.
<If the value of left operand is less than the value of right operand, then condition becomes true.(a < b) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.(a >= b) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true.(a <= b) is true.

Let’s now work in jupyter notebook through quick examples of each of these.

Python Statements

In this post we will be doing a quick overview of Python Statements. This post will emphasize differences between Python and other languages such as C++.

There are two reasons we take this approach for learning the context of Python Statements:

  • If you are coming from a different language this will rapidly accelerate your understanding of Python.
  • Learning about statements will allow you to be able to read other languages more easily in the future.

Python vs Other Languages

Let’s create a simple statement that says: “If a is greater than b, assign 2 to a and 4 to b”

Take a look at these two if statements (we will learn about building out if statements soon).

Version 1 (Other Languages)

if (a>b){
a = 2;
b = 4;

Version 2 (Python)

if a>b:
a = 2
b = 4

You’ll notice that Python is less cluttered and much more readable than the first version. How does Python manage this?

Let’s walk through the main differences:

Python gets rid of () and {} by incorporating two main factors: a colon and whitespace. The statement is ended with a colon, and whitespace is used (indentation) to describe what takes place in case of the statement.

Another major difference is the lack of semicolons in Python. Semicolons are used to denote statement endings in many other languages, but in Python, the end of a line is the same as the end of a statement.

Lastly, to end this brief overview of differences, let’s take a closer look at indentation syntax in Python vs other languages:


Here is some pseudo-code to indicate the use of whitespace and indentation in Python:

Other Languages

if (x)


if x:
    if y:

Note how Python is so heavily driven by code indentation and whitespace. This means that code readability is a core part of the design of the Python language.

Now let’s start diving deeper by coding these sort of statements in Python!

Time to code!