Variables and Data Types#

In the world of programming, our focus is on understanding things through a computer’s eyes, like looking at objects in a special way. To make this happen, we need to pick out the most crucial details about these objects that will help us answer our questions. This is where the magic of Python comes in. Python is like a toolkit full of different kinds of containers. Some containers are like boxes for holding whole numbers, others are for numbers with decimal points, and some are for words and sentences. These containers help us gather and sort the important details about objects, and each type of container is designed to hold a different kind of detail. So, if we’re trying to understand things in the world using Python, we use these containers to hold the important bits of information, like the numbers and words that matter most for the questions we want to answer.

Numbers#

We can use numbers in Python in obvious way. To demonstrate this, we will use the print function to output some examples. Simply specify a number and print it to the standard output.

print(42)
Hide code cell output
42

This is an example of an integer number. In Python, integer numbers have the type int. Numbers with a fractional part (e.g. 42.3, 1.0) have the type float. You can always get the type of an object in Python by using the type function. Let’s check out a couple of examples:

print(type(42))
print(type(42.3))
Hide code cell output
<class 'int'>
<class 'float'>

We can perform various operations with numbers (both for int and float types), which will work as expected from a mathematical point of view:

  • + - addition

  • - - difference

  • * - multiplication

  • / - division

  • // - integer division (rounds down for both positive and negative numbers)

  • % - modulo operation

  • ** - exponentiation

  • () - we can enforce order of operations with parentheses

Now let’s look usage examples:

print(3 + 5)
print(8 - 3)
print(3 * 5)
print(8 / 3)
print(8 // 3)
print(8 % 5)
print(3**5)
print((3 + 5) * 8)
Hide code cell output
8
5
15
2.6666666666666665
2
3
243
64

Strings#

We already saw the first example of strings in python in our first program: "I'm Python. Nice to meet you!". Strings are used in python to handle and operate with texts. There are couple different ways how you can specify string in python:

  • 'content' - single quotes

  • "content" - double quotes

  • """content""" or '''content''' - triple quotes (used for multiline strings)

Examples:

print("Line")
print('Also line')
print("""First Line
Second Line
Third Line""")
Hide code cell output
Line
Also line
First Line
Second Line
Third Line

If you need to use ' or " inside of your string there are examples how to do it:

print("I'm a human")
print('I\'m a human') # Here we use enclosing for `'` symbol
print('I am a "human"') 
print("I am a \"human\"") 
Hide code cell output
I'm a human
I'm a human
I am a "human"
I am a "human"

I would recommend use enclosing (\' and \").

Operations on strings#

  • Concatenation. We can merge strings into one by using the + operator or by simply putting them one after the other. Here are some examples:

print("Hello" " World!")
print("Hello" + " World!")
Hide code cell output
Hello World!
Hello World!
  • String repeation. If you need to repeat a string, you can use the operator, followed by the desired number of repeats. Here is an example:

print("You talking to me? " * 3)
Hide code cell output
You talking to me? You talking to me? You talking to me? 

These are exaples of basic string operations further in the course we will see some other things that we could do with strings.

Logical data type#

In programming, we often encounter situations that demand binary decision. When I think about it I always recall the moment from “Matrix” movie where there is the Neo making decision between red and blue pills. Logical data types serve as the digital manifestation of this choice. To manipulate between with different decisions python has bool data type with two objects True and False. They enable us to represent two distinct possibilities: true or false.

This data type will assist us in making decisions within our programs. There will be situations where we need to execute one part of the code if certain statements are true, and another part if they are false.

Logical operators#

For now, let’s review some basic logical operations:

  • and - logical and

  • or - logical or

  • not - logical not

# and
print("True and True:", True and True)
print("True and False:", True and False)
print("False and True:", False and True)
print("False and False:", False and False)

# or
print("True or True:", True or True)
print("True or False:", True or False)
print("False or True:", False or True)
print("False or False:", False or False)

# not
print("not True:", not True)
print("not False:", not False)
Hide code cell output
True and True: True
True and False: False
False and True: False
False and False: False
True or True: True
True or False: True
False or True: True
False or False: False
not True: False
not False: True

Comparison operators#

You can get bool data type as result after performing comparison of other objects. Here is the list of comparison operators:

  • == - equal

  • != - no equal

  • > - greater than

  • < - less than

  • >= - greater than or equal to

  • <= - less than or equal to

Let’s take a look on couple examples:

print(1 == 0)
print(1 != 0)
print(1 > 0)
print(1 <= 0)

# it is possible to compare strings as well
print("Hello" == "Hello")
print("alpha" <= "beta")
Hide code cell output
False
True
True
False
True
True

Precedence of Operators#

Okay, we have looked at some operations separately. In reality, we often need to combine them in a more complex way. To do this, we need to remember the precedence of operators. The following table summarizes the operator precedence from highest to lowest:

Table 1 Precedence of Operators#

Level

Operators

7 (high)

**

6

* / // %

5

+ -

4

== != <= >= > <

3

not

2

and

1 (low)

or

As usual you can enforce order of operations with parentheses ().

Variables#

Okay, so far we have learned how to create objects in Python and print them immediately. However, we usually want to store objects and reuse them in our code. To do this, we use variables in Python. We can create a specific name and assign it to a specific object in Python. Here is an example:

name = "Vladimir"
print("Hello " + name)
print(name + ", how are you today?")
Hide code cell output
Hello Vladimir
Vladimir, how are you today?

Note

In programming, the concept of variables is very important because it allows for the reuse of specific parts of code. Throughout our course, we will also cover other concepts that enable code reuse. Code reuse refers to the practice of writing code in a way that it can be used in multiple parts of a program or even in different programs. This promotes efficiency, reduces redundancy, and simplifies maintenance and debugging. We will also cover functions and packages, which demonstrate the beauty of code reuse further along in the course.

A commonly used analogy for variables is to think of them as named boxes where we can store our objects. In the example above, we can think that we have a box named name that contains the string "Vladimir".

Some other examples of possible operations over variables:

value_1 = 101
value_2 = 42

print(value_1 + value_2)
value_1 = 0 # we can assign a new value to a variable
print(value_1)
Hide code cell output
143
0

In case you need to assign multiple variables at once you can use the following syntax:

x, y, z = "User", 1, True
print(x, y, z)
Hide code cell output
User 1 True

In Python, it is very easy to swap the values of two variables. While other programming languages often require an additional variable to accomplish this task, Python makes it much simpler:

x, y = 1, 2
print("x and y before swap:", x, y)
x, y = y, x
print("x and y after swap:", x, y)
Hide code cell output
x and y before swap: 1 2
x and y after swap: 2 1

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)

  • A variable name cannot be any of the Python keywords.

Type Conversion#

Sometimes, you need to change the way a computer understands data. Imagine you have two pieces of text, "4" and "2" and you want to put them together like words. You can do this with a plus + operator, like "4" + "2" which makes "42".

But what if you want the computer to treat these like numbers instead of just text? That’s when Python Type Conversions come to the rescue. Let’s look at some everyday examples of how to do this.

int to float#

You can convert an integer to a floating-point number using the float() function. For example:

integer_num = 5
print(integer_num)
float_num = float(integer_num)
print(float_num)
Hide code cell output
5
5.0

float to int#

To convert a floating-point number to an integer, you can use the int() function. Be aware that this truncates the decimal part, not rounding. For example:

float_num = 3.14
print("float_num =", float_num)
integer_num = int(float_num)  # int_num will be 3
print("int_num =", integer_num)
Hide code cell output
float_num = 3.14
int_num = 3

str to int or float#

You can convert a string containing a numeric value to an integer or float using int() or float(). For example:

num_str = "42"
int_num = int(num_str)
float_num = float(num_str)
print("int_num =", int_num)
print("float_num =", float_num)
Hide code cell output
int_num = 42
float_num = 42.0

But if your string doesn’t represent number then you will get an error if you try to convert it to numerical data type:

line = "xyz"
int_num = int(line)
float_num = float(line)
print("int_num =", int_num)
print("float_num =", float_num)

You shoud get the following error:

ValueError: invalid literal for int() with base 10: 'xyz'

int or float to str#

To convert an integer or float object to a string, you can use the str() function. For example:

int_num = 42
float_num = 3.1415
str_int = str(int_num)
str_float = str(float_num)
print("type(str_int):", type(str_int), " str_int = ", str_int)
print("type(str_float):", type(str_float), " str_float = ", str_float)
Hide code cell output
type(str_int): <class 'str'>  str_int =  42
type(str_float): <class 'str'>  str_float =  3.1415

bool Truthy and Falsy values#

To change an object into a logical data type, you can use the bool() function. Here’s a simple rule for how this conversion works:

  • If you have 0, 0.0, or an empty text "", they turn into False.

  • For all other values, they become True.

This rule helps you understand how different values are seen by the computer in a true or false way. Examples:

print(bool(0), bool(0.0), bool(""))
print(bool(101), bool(-10), bool("ABC"))
Hide code cell output
False False False
True True True

When certain values act like they are True in a computer’s eyes, we call them Truthy. Similarly, when values act like they are False we call them Falsy. Understanding these concepts is important when dealing with conditions and decisions in programming.