Python: Learn the Basics FAST From Python Programming Experts (2015)
Chapter 5: Python Programming – Variables and Types
The Python language is heavily object-focused, and is not “typed statically.” The programmer doesn’t have to specify variables before he can use them, or define their type. We can say that each variable in Python is a standalone object.
This chapter will discuss some of these basic variable types.
This data type keeps numerical values. Number data objects are generated when you set a value to them. Like this:
Var1 = 2
Var2 = 20
You also have the option to remove the connection to a number object by utilizing the del command.
You can remove multiple objects (or just one single object) by employing the del statement.
del typ_x, typ_y
Currently, the Python language supports four types of numerical objects:
Floating point real values
Python strings are defined as an adjacent group of characters displayed in the quotation marks. This programming language lets you use pairs of double or single quotes. You can obtain the subgroups of strings by using a slice operator ([ : ] and [ ]).
The “+” symbol is the operator for combining strings, while the “*” sign is used for repetition.
Python lists are known to be the most flexible data type in this programming language. A list is a group of characters that contains objects enclosed within brackets and are separated by commas. In some way, Python lists are similar to “arrays” in the C language.
The values inside a list can be taken by using a slice operator. Similar to Python strings, the “+” symbol is used for mixing (concatenation) purposes while for repetition, the asterisk “*” is employed.
Because of their importance in the Python language, the next chapter will further discuss their characteristics.
A dictionary in the Python language works like the related hashes or arrays used in Perl. The key of a dictionary can be any type of Python variables, but strings and numbers are most commonly used.
These dictionaries are enclosed within curly brackets. Values, on the other hand, can be assigned and obtained by using the square brackets.
This is a data type similar to a Python list. It consists of a group of values disjointed by commas. However, unlike Python lists, these tuples are expressed inside parentheses. This data type is considered as a read-only list. It cannot be modified or updated.
Data Type Conversion
You may have to implement conversions between the supported variable types. To do this, you just have to utilize the type title as a function.
Multiple pre-installed functions can be employed to complete data conversions from one type to another. These built-in functions produce a new object displaying the value.