Variables in Python and naming them

First, we will look upon the different types of data types and variables that are present in Python.

Here is a brief list ( we will look in detail at each one later on)-

  • Numbers – Integers and Floats
  • Strings
  • Boolean
  • Lists
  • Tuples
  • Dictionaries
  • Sets

Integers and Floats are self-explanatory – They are variables containing integer and float values.

Strings denote a group of characters (it’s just a list of characters).

List is kind of a dynamic array; you can manipulate it, decrease and increase it’s size using it’s various methods (Mutable; we will see what this word means).

Tuples are lists that cannot be altered.

Dictionaries contain key-value pairs – we can quickly grab objects without needing to know it’s index locations.

sets are an unordered collection of unique elements.

Confused? Don’t be! As I said before, we are going to go in depth of each data type.

Python – A dynamic language

Python is a dynamic language, which means that the variable (name) is bound only to an object and not a type.

So, if a variable initially stored integers, we can also store strings or lists or any other data type in it, and our program will still work without throwing any error.

So, we do not need to declare the type of variable beforehand; it changes dynamically every time we reassign it.

Casting:

However, there will be times when we want our variable to be or become a specific data type, and during such times we do ‘Casting’.

What is Casting, you ask? Well, casting can be thought of as turning one object of a type to some other object of a different type.

Mainly, we use the following three things (called constructor functions, this comes under Object Oriented Programming and you don’t have to worry about it for now)-

  • int() – Used to cast a variable of float type or string type containing numbers into a int type. It will round up the float value and represent the string value as it is.
  • float() – Used to cast a variable of int type or string type containing numbers into a float type.
  • str() – Used to cast a variable of int type or float type into a string type.

Variable naming rules:

There are some rules that must be followed while naming a variable in Python. They are listed below –

  • The variable name must begin with an alphabet (lower or upper case) or an underscore ( _ ).
  • We cannot use keywords as variable names. To list the keywords in Python, import the keyword module and run the command keyword.kwlist in the Python terminal.
  • The variable names are case sensitive, so be careful.

That’s it folks! In the next tutorial, we will look at the output and input functions, and see how to open python files in the terminal.

Variables in Python and naming them