Python Series Part 2: Python Basics

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
364
Reaction score
403
Credits
13,257
Python uses many keywords and variables. You'll need to keep in mind the difference between the two and not confuse them.

If you confuse them, then the Integrated Development Environment (IDE) should give you an error, especially when you run the code.

Keywords

Keywords are actual words that are used for commands, sometimes these are called identifiers. For example, in the previous article, we used the command 'print'. Since this is a command, it is a keyword and cannot be used as a variable.

Some words are keywords, but not a command. These words would be something like 'else' or 'elif' that goes with the 'if' keyword.

Variables

Variables are used to represent other characters or numbers. Algebra is a great example. When 'x=2' and 'y=5'. These are variables.

Variables can also be 'strings' of characters, such as 'c=Linux'. Or even a combination of letters and numbers.

Code Test

In the Visual Studio Code IDE, it will recognize keywords and variables.

Let's give this a test and see what happens.

On the first line, type in 'xyz="Linux.org"'. Since this is a string variable, we have to place double-quotes around the string. We do not want to confuse the IDE by making it think we are assigning one variable to another, such as 'xyz=Linux.org'. Here, 'xyz' and 'Linux.org' are both variables.

So, now we have the text 'Linux.org' assigned to the variable 'xyz'.

On the next line, type in 'print ("xy'. Stop there. You should see a box appear to help you find keywords and variables, as shown in Figure 1.

Figure 1.JPG

Figure 1

You can see at the top of the box is the list to match any keywords or variables that start with 'xy'. The bottom of the box shows the syntax for the 'print' command. These boxes can be helpful to make sure you do not type in an incorrect keyword or variable. If an option appears, you can highlight it and press ENTER to auto fill the selection. The second line should be 'print (xyz)'.

The auto fill feature is one of the many reasons to use an IDE and not a simple text editor for coding. The auto-fill feature works for Python, since that is what we selected when we set up Visual Studio Code.

You can press the 'Play' button to run the code and see that it prints the text. In this manner, it is simple to go back and change the string to other words to be printed.

We can see how it works, but let's look at breaking it. Change the variable name on both lines to 'for'. We use the 'for' keyword in a 'for..next' loop. So, the word 'for' is a keyword.

After you change it, try to run it. You should get a Syntax Error.

Change the keyword on both lines to 'For' and run the code. It should work. Why does it work now?

Keywords are case-sensitive. The keyword is 'for' so you can use 'For' as a variable. Keep in mind that the variable name 'For' is also different from 'FOr' and 'FOR'.

You can see that the help box that appears can help prevent typos.

Variables can include numbers, but cannot start with a number. Variable names can also start with and underscore ('_').

Variables cannot contain a space. A space would signify the end of a variable and the beginning of another. Along with spaces, you cannot use special characters in a variable name.

In some programming languages, you need to pre-define variable types. Such as the variable number will be an integer. So any value placed into the variable 'number' should be numbers and can perform arithmetic operations. For Python, you do not need to pre-define the contents of a variable.

Another type of variable is a constant. Setting a variable as a constant will let you assign a value to variable, but every time we start the code, the value will go back to the initial value that is set.

Variable Data Types

We can see the data type of variable by printing out the command 'type(variable)'. So, if we had a variable 'a' and we wanted to know the data type, the command is 'print (type(a))'.

Of course, we can make it a little fancier, but this is easy. I the following code, there will be a variable set and then the name and print the data type:

Code:
f=3.14159
print ("Float ", type(f))
i=1
print ("Integer ", type(i))
c=1+3j
print ("Complex ", type(c))
s="Linux"
print ("String ", type(s))
b=True
print ("Boolean ", type(b))
l=["Linux", "Windows", "macOS"]
print ("List ", type(l))

The output is:

Float <class 'float'>
Integer <class 'int'>
Complex <class 'complex'>
String <class 'str'>
Boolean <class 'bool'>
List <class 'list'>


Of course, I give each one away in the text string that is printed before the type, but you get the idea.

The 'float' data type has a decimal. The 'integer' is a whole number. 'Complex' numbers contain a real number, '1', and an imaginary number, '3j'. A 'string' is text. The 'boolean' type is 'True' or 'False', and it has to start with a capital letter. Another data type listed is 'list', which is assigned with brackets. You can think of the List data type as similar to an array. We can get into those in a later article.

After setting a variable as a data type, you can just assign it a new value and it should change to that data type. For example, if I assign the variable 'b' a value of '1', it is of type 'integer'. Then if I assign it the value of 'Linux', the type is now a 'string'.

If you perform arithmetic operations to the variable, you can change the data type. An example would be that we assign a value of '1'. The variable is an integer. We then use the operation of multiplying the variable times '1.1' in the line 'a=(a*1.1)'. This takes the value in 'a' and multiplies it by '1.1' and then reassigns it back to the variable 'a'. The variable is now of a data type 'float'. If we should perform another mathematical operation to undo the changes, divide by '1.1', the new value is '1.0' and it is still the data type float.

Input Data

So far, we have assigned values to variables in the coding. What if we would want to ask the user for information?

We can use the 'input' command to get data from the user.

Let's try something basic, such as asking for the name. The variable ‘name’ receives the data. We would then print the name with the data type. The code would be:

Code:
name=input("What is your first name? ")
print ("Hello ", name)
print (name, "is of data type: ", type(name))

The 'input' command uses the prompt for the user, 'What is your first name? '. The variable ‘name’ receives the string entered.

Notice, I said 'the string' entered is placed into a variable. Anything you type, even if you enter an integer, would be of data type 'string'. We can reassign data types, but again, this is for another article.

After you run the code, click at the bottom, which is a 'terminal' where the programs run. Type in your name when prompted and see the results.

Conclusion

You should know about variables so you can store information and retrieve it again.

You should understand some of the data types in Python. You can determine the data types if you ever try to perform an operation that gives you an error. For example, you cannot perform mathematical operations on a string. If you try, you will get an error.

For help with data input, you can retrieve information from the user.

This may seem basic to some readers, but we must start at the beginning. We can get along further and start making more interesting programs once we have a better understanding of the language.
 
Last edited:



Members online


Top