Introduction to Python: variables and number data types

Enoch Lamikanra
4 min readJan 24, 2021

Variables are simply names for something e.g a collection of strings, integers e.t.c. There are different methods of naming variables that can be adopted when using a variable name with multiple words to enhance code readability.

Methods of naming multi-word variables

  • Snake case

Place an underscore(s) in-between the words.

  • Camel case

The first letter of each word is written in uppercase except for the first word.

  • Pascal case

The first letter of each word alone will be written in uppercase.

Assigning value(s) to multiple variables

  • Multiples values can be assigned to multiple variables as shown below
  • A value can also be assigned to multiple variables

Output variables

To print variables, a python print statement may be used to return the variables, this allows room for combining both text and variable(s).

  • In python, “+” can be used to concatenate variables and strings;
  • “+” character can also concatenate two or more variables together

Global variables

These are variables that can generally be used anywhere in the code i.e inside or outside functions once declared.

Local variables

These are variables that can only be used outside functions or used within a function, only if the variable was created inside the function. If a local variable is called outside its function, it returns a “NameError”.

However, there’s a workaround that allows variables created within a function to become a global variable by calling the “global” keyword, followed by the variable after the function has been defined.

Dos and don’ts of naming variables

  1. Python is case sensitive, when naming variables “name” is different from “Name”. Calling variables with names that differ from the intended name due to the letter case, will return a “NameError’ which indicates that name used to call that variable hasn’t been defined.

2. In python, only identical data types can be concatenated. Hence, a string and an integer should not be concatenated, as it will return “type error”. However, the data type of a new or pre-existing variable can be modified by casting. This allows variables with different data types to be concatenated.

Numerical data types

Data types are the classification or categorization of data items (Programiz, n.d.). Pythons built-in numeric data types include

· Int

Integers are positive or negative whole numbers without decimal points

·Float

These are similar to integers, the only difference being the presence of decimal points.

· Complex

Complex numbers are specified as <real part>+<imaginary part>j (Sturtz, 2021). To call real part, use variable.real, for imaginary variable.imaginary.

References

(n.d.). Retrieved from Programiz: https://www.programiz.com/python-programming/numbers#:~:text=Number%20Data%20Type%20in%20Python,Python%20supports%20integers&text=They%20are%20defined%20as%20int,is%20a%20floating%2Dpoint%20number.

Sturtz, J. (n.d.). Retrieved from Real Python: https://realpython.com/python-data-types/#integers

--

--