Numbers

Types of Numbers

We are well aware of different types of numbers, such as integers, real numbers, decimals, whole numbers, etc. In Python, we have 3 different numeric types to work with. They are integers (referred to as int), floating-point numbers (referred to as float), and complex numbers.

Integers are the standard numbers we use e.g., 1, 3, 7, 101, 1001, etc. In Python we can declare a variable with an integer by simply following the syntax:

<variable_name> = <desired_int_value>

Let’s try out an example, open up an empty notepad and write down the below code.

integer_variable = 7

print("integer_variable has: ", integer_variable)

Once you’ve written this down, save this file with the name “main” and its extension as “.py”. To change the extension (from txt to py), select the “Save as type:”, and from its drop-down list select the “All Files”, as shown below.

Now head on over to the directory, where you have saved this file and you should see a file as shown below.

 

So far, we have written just two lines of code, but now we’ve come to the exciting part, which is the execution of our code. We will be using our command prompt to execute this file, so open up your command prompt. But how do we execute the file from our command prompt? There are two ways to reach our “main.py” file from our command prompt. 

METHOD 1

Using the command prompt, we jump into the directory where our python file is stored.

  • The image below shows where I have saved my main.py file

  • First, we need to copy the directory of where our file is stored.
  • Then we need to open up our command prompt and enter the following command:
    • cd D:\Projects\Internship 

You might be thinking what is ‘cd’? It is a command used in the command prompt which stands for “change directory”.

NOTE: My “main.py” is saved in another drive. So, if you are trying to access a file that is stored in another drive (i.e., other than the C drive), first enter the drive letter followed by a ‘:’ and then paste the above cd command.

METHOD 2

We go to our directory and jump into the command prompt from there.

  • In the directory where our file is saved, we click on the file path, type cmd, and hit enter.

  • The command prompt should pop up with the following:

NOTE: It will be a different directory, depending on where you saved your file.

To execute a Python file the syntax used is 

  • python <file_name>.py

Keeping that in mind, we have to type the command shown below and then press enter:

  • python main.py

You should see the following on your command prompt with the required output we designed it to print.

Remember that we added a print statement as our second line of code? That is what prints the output onto the terminal (aka the command prompt).

NOTE: Python 3 specifically has no limitation such as a maximum number. Instead, it depends on the available memory and can expand accordingly.

Now let us try running the code with another numeric type, specifically the floating-point number (or simply known as float), which represents all decimal numbers.

Right-click the main.py file and select the “Open With” option and choose “Notepad”. Then go ahead and edit the file as shown below. (Feel free to experiment with a different value for the variable or use your own unique variable name)

float_variable = 7.5

print("float_variable has: ", float_variable)

Run the code (in any one of the methods) as shown earlier and you should get this output.

Finally, we have the complex number type. If you are not familiar with complex numbers, it is a type of number that comprises two parts, real numbers and an imaginary unit (denoted with i) and they satisfy the condition i2 = -1. They are expressed in the form a + bi, where a and b are real numbers and i is the imaginary number. 

complex_variable = 5 + 3j

print("complex_variable has: ", complex_variable

When we run the code, we see the following output.

 

NOTE: The naming convention in Python uses the letter j instead of i, this is to avoid various potential errors.

We can execute various mathematical operations with these different numeric types. Below I have written a couple of operations we could do.

# A set of variables

# Integer variables
a = 15
b = 17

# Float variables
c = 5.0
d = 15.0

# Complex variables
e = 5 + 6j
f = 6 + 7j

# Operations

print("Addition")
print("a + b = ", a + b)
print("e + f = ", e + f)

print("")  # we will use this statement to leave an extra line in the output
print("Subtraction")
print("d - c = ", d - c)
print("f - e = ", f - e)

print("")  # we will use this statement to leave an extra line in the output
print("Multiplication")
print("a * b = ", a * b)
print("e * f = ", e * f)

print("")  # we will use this statement to leave an extra line in the output
print("Division")
print("d / c = ", d / c)

print("")  # we will use this statement to leave an extra line in the output
print("Modulus")
print("b % a = ", b % a)

# What will happen if we use varying numeric types in an operation
print("")  # we will use this statement to leave an extra line in the output
print("Using different data types")
print("a + c = ", a + c)
# Python will automatically convert the int to float and produce the output as float this is known as type conversion

The output would look like this

Working with Negative Numbers

To use negative numbers in Python is as easy as it can get. We simply have to declare a variable(s) with the negative number(s) we need.

x = -115

print(x)  # Prints -115

What have we learned?

  • What is the syntax of creating a variable?
  • What are the different numeric types in Python?
  • How to change directories using a command prompt?
  • How to run a Python file?
  • What are the different operations we can do in Python?
  • What are complex numbers?
  • What is type conversion?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments