Intro to Python Programming (Part 2) - Python Interpreters
2021-05-17 | By Maker.io Staff
The last part of this series discussed the basics of the Python programming language, and it also addressed a few theoretical aspects of the language. This article explains variables and simple operations, and it also demonstrates how to use the interpreter for quick prototyping.
Installing a Python Interpreter on Your Computer
Before you begin, you’ll have to install the Python runtime environment on the machine you want to use for running Python applications. You can skip this step if you’re using a Raspberry Pi, as the latest versions of the Raspbian OS come with Python pre-installed. Otherwise, head over to the Python website and download the latest release of Python 3 for your operating system. Then, follow the instructions supplied with the downloaded package to install the runtime environment.
Once you’ve downloaded the correct package for your operating system and installed the Python runtime environment, you can test whether everything is working as intended by opening a new console window and typing ‘python3’. You should see that the Python interpreter starts up and be able to type simple expressions like these:
Note that I’m using a simple text editor and the console application throughout this tutorial to make it as easy as possible for everyone to follow along regardless of their OS. If you want to use a graphical environment, you can choose from many different Python IDEs, such as PyCharm.
Creating Variables in Python
As mentioned in the introductory article for this series, variables don’t have a fixed data type in Python. Instead, Python is a so-called dynamically typed programming language, which means variables have the data type of their assigned value. Therefore, it’s enough to specify the name of a variable followed by its value to define a new variable:
numeric_variable = 10 some_text = "This is a string!" character_var = 'c'
Note that you can’t define a variable without assigning a value to it. You can also check the currently assigned type of variable. This type can change at any time, and it only depends on the value stored in the variable:
While this sounds like a great thing for beginners, it’s strongly suggested that you don’t dynamically change variable types in your programs, as this can be confusing as you’re building things up in the long run. It’s better to store values with a single data type in a variable and create new variables as needed.
Using Operators to Manipulate and Compare Variables
Storing values in variables isn’t very useful when you can’t do anything with these values. Just like any other programming language, Python supports the comparison operators that you’d typically expect:
x = 200 y = 25 x < y # Test if x is less than y x > y # Test if x is greater than y x == y # Test if x and y are equal x != y #Test if x and y are not equal x <= y # Test if x is less than or equals y x >= y # Test if x is greater than or equals y
The hash symbol denotes that everything that follows it in that line is a comment and that the compiler and interpreter should ignore the rest of that line. Comments allow you to explain what parts of your code do, and they don’t change the behavior of a program. Keep in mind that you can store the result of a comparison in a new variable and process it later. Note that Python allows you to not only compare numerical values. It is also possible to compare strings this way to determine which one is longer:
t1 = "Hello World!" t2 = "This is a longer string..." t1 < t2 # true t2 < t1 # false
As for primitive operations, Python supports the ones you’d expect. Examples are numeric addition, subtraction, multiplication, division, and powers. Besides those, some binary operators are also available for other data types. It is, for example, also possible to add strings (e.g., the append operation). However, you can’t subtract a string from the other:
Python also supports logical operations such as AND, NOT, and OR. These allow you to chain multiple comparisons or the results of such comparison operations together:
x = 200 y = 25 result = (x <= y) # Store the result of the comparison in a new variable result2 = (x > y or x > y/2) # This comparison uses the logical OR operation print(result) print(result2) print(result and result2) # Combine the two results by using the AND operator
Recommended Reading
- Intro to Python Programming (Part 1) - Getting Started
- Intro to Python Programming (Part 3) - Python Lists
- Intro to Python Programming (Part 4) - Call Functions and If-Statements
- Intro to Python for Makers (Part 5) - Custom Functions
- Intro to Python for Makers (Part 6) - Classes and Objects
Summary
This article has introduced you to primitive variables and basic operations in Python. Keep in mind that Python is a dynamically typed language. As a result, you don’t explicitly specify the data type of a variable. Instead, the value stored in the variable determines its type. This dynamic type can change throughout a program.
You can use simple operators, such as the addition operator, to combine variables and immediate values. You can also employ comparison operators, such as equals, to compare values. It’s possible to store the results of such comparisons in new variables, and Python also allows you to combine multiple comparison operations by employing logical operators.
In the next article in this series, we’ll look at how Python lists work and explore how they can help your projects!
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum