Python is not “statically typed” and is entirely object-oriented. Variables do not require type declaration or declaration before use. In Python, each variable is an object. Let’s read this article to know about Python variables.
Python variables are memory locations where you store a value. In Python program, a variable provides the computer with data to process. Each value has a datatype. You can use any name or even an alphabet, such as a, bb, or abc to declare a variable.
Python Variable Naming Rules:
- The variable name must begin with an alphabetic character (a-zA-Z) or an underscore (_).
- Only the underscore (_) and no other special characters are permitted in variable names.
- Case sensitive when using variables.
- Numbers can appear in variable names but not at the start.
- Python should not use the word “variable” as a keyword.
- Reserved words are another name for keywords.
In Python, the type of data to be used for declaring, creating, and running mathematical functions on the input provided by the user determines the program’s variable types altogether. The code typically uses a few fundamental variable types. For example, the integer for numerical values, the float variable type for decimal numeric variables, the string for character representation, the boolean for true/false and 0/1 values, and the list for a list of values.
These types of variables include int, string, float, bool, char, and many others.
Floats are decimal numbers, while integers are numbers. Defining an integer or any other types in Python is pretty simple. You just need to name the type variable and assign a number to it.
An integer cannot have a decimal point and can only be positive, negative, or 0. They support all types of mathematical and arithmetical operations, including addition, subtraction, getting the residual, absolute value, and more, and they have limitless precision. Floats are decimal numbers. They are compatible with integer operations.
Strings are used to symbolize the text. In Python 2, it is ASCII text, but it is Unicode text in Python 3. You can use single or double quotes or three times the quotes to define the strings.
A boolean value is either True or False. Any variable can be set to true or false by declaring it to be a Boolean. To assign True or False to a variable, you just need to type its name. The words True and False both begin with capital letters T and F. If you convert a Boolean value to an integer, it will return a value of 1 if it is True and a value of 0 if it is False. Though you will only receive a textual representation if you convert True or False to a string, i.e., a string containing the value True or False.
None indicates that a variable has been specified so that the developers type the variable’s name anywhere without assigning it a value. In other languages, it is equivalent to null.
You can write a variable name and give it empty square brackets to define a list in Python. An empty Python list has just been created there.
Arrays in many other languages are quite similar to lists in Python, but lists have some extra advantages. A single list can contain different data types.
If you want to add more information to a single individual than just their name, like a person id, but you still want the person id to be linked to the name, you can use a dictionary. Python dictionaries have both keys and values.
A pair, or a key-value pair, is made of a key and a value. One value will be assigned to each key. You can add any type you want, just like with lists. When you need to keep some structured data, dictionaries are incredibly helpful.
In this article, we have shown you Python variable types and explained their differences. If you want to expand your knowledge, let’s learn and share knowledge about IT on LearnshareIT.