Python: Variables & Numbers

The Journey From Python to Machine Learning. (Python: Variables & Numbers)

Himanshu Raj
HSR Hi-Tech Solutions
5 min readMay 13, 2020

--

Let’s take another step in this journey. We will first learn a few basics things in Python.

You know it well that any programming language is a medium in communication between humans and computers. As in normal conversation, we say or ask something to another person and expect some response or answer from that person. Here we will do the same with our computer, language for our conversation will be Python.

You might have faced the issue of hesitation “how to start the conversation?” at some point in your life. Don’t worry! Here I am to help you out in having a conversation with your computer or laptop. Just assume me as one of those friends who help you out in having a conversation with your crush.

* Input/Output

Problem no. 1: How to start the conversation? (i.e, How to give input to the system?)

Tip 1: As you always find out what makes your crush comfortable before having a conversation with him/her. You have to find out what is important for Python. So always remember your new crush only feels comfortable in Python. So you must have Python 3 installed in your system. Open either Jupyter notebook and create a new Python 3 notebook or open Python 3 IDLE.

Tip 2: Syntax for input we use “input()” (input function). This will wait for user input when we will run this syntax.

Tip 3:Syntax for Output we use “print()” (print function). This will print out result on the screen. Let’s have a look on the code given below.

Note: For putting a single line comment in Python we use # before the line.

* 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.

Variables are like spending some time with your crush and gathering a few happy moments of your life. You can carve memories with your crush freely but maintain some rules.

Rules of declaring a variable:

  • 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 never consists of special characters or symbols (<>/’;][\=_)(*&^%$#@!)

Note: You don’t need to put data type of any variable before the declaration. You can reassign a new value to the Python variable.

You can assign any data type to a variable.

Before proceeding towards “numbers” let’s have a short introduction to data types of Python.

DATA TYPES: A data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data.

Python has 6 data types:

  • Numbers
  • Strings
  • List
  • Dictionary
  • Tuple
  • Set

Watch the video for detailed information about Python Data Types.

* Numbers

Now it’s time to gather some knowledge about the new interest of your crush.

Number data types store numeric values. They are immutable data types, which means that changing the value of a number of data type results in a newly allocated object.

Python supports four different numerical types −

  • int (signed integers) − They are often called just integers or int. It may be positive or negative whole numbers with no decimal point.
  • float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
  • complex (complex numbers) − are of the form x+yJ, where x and y are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is x, and the imaginary part is y. Complex numbers are not used much in Python programming.

We can perform all arithmetic operations on numbers. Symbols used for arithmetic operations are:

  • Addition (+)
  • Subtraction ( — )
  • Multiplication ( * )
  • Division ( / )
  • Modulo ( % )

The modulo operation — is a way to determine the remainder of a division operation. Instead of returning the result of the division, the modulo operation returns the whole number remainder.

You can also perform type conversion on numbers.

For getting the data type of any variable we just use syntax “type(-variable_name-)

  • Type int(a) to convert a to a plain integer.
  • Type float(x) to convert a to a floating-point number.
  • Type complex(a) to convert a to a complex number with real part a and imaginary part zero.
  • Type complex(a, b) to convert x and y to a complex number with real part a and imaginary part b. a and b are numeric expressions.

You have come across a few interests of your crush. Now it’s time to take a break. I will let you know other interests (data types) in the next blog.

Here is your Jupyter notebook file — click here

You may also take help from the video given below.

For any queries or doubts just write in the response section. If you like this blog useful please clap for appreciation.

You can join our student community for the latest updates on technologies and programming languages. If you have any queries and doubts then write in the response section below.

HSR Hi-Tech Solutions Student Community Important links:

Whatsapp: click here

Telegram: click here

--

--