Python Variables Tutorial With Examples
Python is an interpreted artificial language of dynamic typing whose philosophy emphasizes a syntax that favors readable code. It's a multiparadigm artificial language and available on several platforms.
Python contains a large number of libraries, data types and functions incorporated in the language itself, which help to perform many common tasks without having to program them from scratch. But what really makes it brilliant using it on a Raspberry Pi, is the ability to use GPIO pins to connect the physical world with the digital world .
Variables in Python Programming:
A variable is a site where we store certain information. Depending on the type of information we store (text, numbers, Boolean, etc.), the variable will be of one type or another. For simplicity we will only see the text and numerical variables, since they are the ones that are used in more than 80% of the occasions.
Each variable has to have a name with which to refer to it. Python takes into account whether we write the variable in upper or lower case (what is known as case sensitive). A variable called f1 is not the same as one called F1.
And why do you have to say what you put in each variable? Because depending on what is inside it, one or other things can be done, such as adding two numbers.
In Python these are the most common variables:
Numbers:
In numbers there are two main types, integers (called int) and real numbers (called float). The decimal separator we have to use is the point.
Although if we do not say the type of number that it will contain, Python will try to decide for itself which one is the most appropriate, this sometimes produces errors. My recommendation while you are starting to learn, is to always specify what type of number it is, as it will avoid future frustrations.
Of course, and to avoid confusion, the variable name cannot match the names of the python "commands" (if, for, etc.). Nor can we use variable names with tildes or with ñ.
Text:
The variables that store text are called strings (str). They must be in single quotes (') or double quotes («), or if the text occupies several lines, between triple double quotes (« »»).
As with numbers, Python assumes that what we enter is a text when it is in quotes, although we can always force it to be interpreted as text using the str command.
Print and Variables:
As we have seen, the print command is very useful for the program to communicate with us. This command shows the text we put, or even the value inside a variable. In the case that we combine text and numbers, we must take the precaution of converting the numbers into text, because if not, Python does not know how the sum of a text and a number is done.
If you are interested in knowing more about Comments in Python language, then get Python training in Bangalore from India's leading E-learning platform.
Comments
Post a Comment