Introduction to Python Part 2

Variables & Data Types, Basic Syntax, and Operators

You are probably here because you read my blog post: Introduction to Python Part 1. If not, and if you are brand new to Python, then stop and read that post now. Otherwise, continue reading.

This post looks at how to handle variables, the different data types, basic Python syntax and important operators. Once, you’ve read through this and have done some practice yourself, you’ll be well on your way to basic Python programming.

If you prefer to follow along with the source code, the headline for each section links to the repository on my Github. Following along there will give a better idea of what the Jupyter notebook interface looks like.

Variables and Data Types:

Variables store information and represent inputs.

Let’s assign the value 5 to variable x with the equality sign and then print the value on a new line by typing x:

x = 5
Output: 5

print(x) will also display the value of x.

Output: 5

Variables can also have new values assigned to them. This means that the latest assignment command is valid and older commands are overwitten. Let’s now assign the value 6 to x:

x = 6
Output: 6

You can also assign multiple variables at one time:

x,y = (5,6)
Output: 5 6

Variables can have different data types.

This includes integers(whole numbers), floats(real numbers with decimals) or booleans(binary variable). To determine the data type of a variable use the type() function on the variable:

Output: int

Using the functions int() and float() will turn a variable into an integer and a float, respectively. Let’s see these in action with variables a and b:

a = 4.2
b = 5

int(a), float(b)
Output: (4, 5.0)

There is another kind of input you can use in Python called strings or text. Since Python may read a word as a variable, it is important to either input text with quotes or assign text to a variable. Lets take a look these options:

Using double quotes or single quotes will return the string with single quotes:

"Hello, Python", 'Hello Python'
Output: ('Hello, Python', 'Hello Python')

Assigning a string to a variable and printing it with the print() function will return the string without any quotes:

z = "Hello, Python"
Output: Hello, Python

You can combine a variable with a number to a string, but will need to convert that variable into a string with the str() function first. Additionally, when combining numbers and text to print, you should leave a space in the quotations before the text, for a space in the printed result:

print(str(x) + " Golden Rings")
Output: 5 Golden Rings

The last thing we should cover with strings is the use of quotes in text. If you’re input text has a single quotation in it, either use double quotes around your text with single quotes inside or use a backslash before the single quote within single quotes:

"I'm Good" , 'I\'m Good'
Output: ("I'm Good", "I'm Good")

If you want to use double quotes in your text, use single quotes around the text, with your double quotes in the text:

'Click "Here"'
Output: 'Click "Here"'

Basic Python Syntax:

Arithmetic Operators:

Arithmetic operators include the addition, subtraction, division and multiplication signs. Let’s start with addition and subtraction which are as simple as using “+” and “-“:

10 + 15
Output: 25
15 - 10
Output: 5

For division, we use the forward slash:

Output: 3.2

You can get the remainder of two numbers with the percentage symbol:

15 % 2
Output: 1

Multiplication uses the asterisks symbol. It is important to note that arithmetic expressions can also be assigned to variables. Let’s look at an example of that here:

x = 5 * 3
Output: 15

One final arithmetic operation that you should know how to input in Python is the power operation. To do this, you have to use two asterisks symbols in between the base and the exponent:

Output: 100

Double Equality Sign:

You may have noticed that the single equality sign is useful for assigning variables. In Python, the use of double equality signs is beneficial for determining equality between two elements. For example:

100 == 100
Output: True
100 == 99
Output: False

Adding Comments to code:

You can add comments to code by starting the line with a hashtag. This would lead to no output:

# this is a comment line with no code

You can also have a cell with a comment and code, which will only output the code:

# this cell has a comment and code
print("Hello, Python")
Output: Hello, Python

Line Continuation:

Sometimes when writing code, you may have code that is too long for one line. In cases like these, you can split your line into two and use a backslash at the end of the first line. This indicates that you will continue the same command on a new line:

(5 + 6) - \
Output: 1

Indexing Elements:

Indexing allows you to extract an element from a variable. For example, if you wanted to extract the letter “y” from “Python”, you would use indexing. Indexing works by putting the name of the variable in quotes followed by brackets with the number or index of the element that you want to select (“Name_of_variable”[index_of_element].)

It’s important to note that in Python, the count starts from 0, so even thought “y” is the second letter in “Python,” its index would be 1:

Output: 'y'


Indents help to break up and define blocks of code. For example, if you were defining a function (which we will cover later), indents help to determine what code belongs where.

The indents in the example function below help indicate that indented lines of code belong to first line. And it helps determine that the print() command that follows does not belong to that function.

def one(x):
    x = 1
    return x
Output: 1

Comparison and Logical Operators:

Comparison Operators are used when you want to compare and evaluate variables or numbers.

As we covered earlier, the double equality sign is used to determine if two elements are equal:

10 == 10, 10 == 15
Output: (True, False)

The exclamation point followed by a single equality sign is used to determine if one element is “not equal to” another element:

10 != 10, 10 != 15
Output: (False, True)

“Greater than” and “less than” are indicated by the arrow symbols:

100 > 50, 100 < 50
Output: (True, False)

“Greater than or equal to” and “less than or equal to” are indicated by the arrow symbols followed by the equality sign:

100 >= 50, 100 <= 50
Output: (True, False)

Logical Operators in Python are AND, NOT and OR.

They are used to compare statements and return boolean values “True” or “False.”

AND checks whether the statements around it are True:

True and True, True and False
Output: (True, False)

OR check for at least one statement that is True:

False or False, True or False
Output: (False, True)

NOT gives the opposite of the given statement:

not True, not False
Output: (False, True)

Logical operators can work on regular statements that are not booleans:

2 > 6 and 10 <=20
Output: False

You can combine logical operators in one command, but it’s important to know the order they are evaluated in: NOT, AND, OR. Let’s look at a couple of examples:

True and not True
Output: False
True and not True or True
Output: True

We just covered a lot of Python programming basics. If you haven’t started practicing these on your own, I suggest you come back to this post with a fresh mind to go over these syntax rules carefully. Once you’ve mastered these, stay tuned for Introduction to Python Part 3 which will cover conditional statements, functions, sequences, and iteration.

2 thoughts on “Introduction to Python Part 2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s