Constants, Variables, and Arithmetic Operations
In this post, you will learn about Python variables, constants, and arithmetic operations. We'll also look at their use cases along with some examples that you can test on this page!
Variables
What is a variable?
A variable is a named location used to store data in the memory. Variables have a unique name know as an identifier. You might find it more useful to think of variables as a container that holds data which can be changed later throughout programming. Python has five standard data types:- Numbers: a numeric value (int, long, float, or complex)
- String: a sequence of characters
- List: a changeable ordered sequence of elements; each element in a list is called an item
- All the items of a list can be of different data type
- Tuple: just like a list, except the elements or length of a tuple can't be changed
- All the items of a tuple can be of different data type
- Dictionary: an unordered collection of data values, used to store data values like a map
Creating a variable
Unlike other programming languages, Python has no command for declaring a variable. It automatically assigns a memory location as we assign the variable with a value. In other words, a variable is created the moment you first assign a value to it. Variables do not need to be declared with any particular type and can even change type after they have been set.
You can use the assignment operator “=” to assign the value to a variable. Let's look at an example:
Assign Value to Multiple Variables
Python allows you to assign values to multiple variables in one line. You can also assign the same value to multiple variables in one line. Here's an example of both:Naming Conventions for Variables
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
Constants
What is a constant?
A constant is a type of variable that holds values, which cannot be changed. In reality, we rarely use constants in Python. Constants are usually declared and assigned on a different module/file. Then, they are imported to the main file.
Assigning value to a constant
In Python, constants are usually declared and assigned on a module. Here, the module means a new file containing variables, functions etc which is imported to main file. To do so, in a file constant.py, define your constants. Then in another file main.py, import the constant and use. For example:Naming Conventions for Constants
The naming conventions for constants are the same as for variables, with a few additional rules:- Use capital letters where possible to declare a constant. For example PI, GRAVITY etc.
- Constants are put into Python modules and meant not be changed.
Arithmetic Operations
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division. They include the following:
- Addition: Adds values on either side of the operator
- Subtraction: Subtracts right hand operand from left hand operand
- Multiplication: Multiplies values on either side of the operator
- Division: Divides left hand operand by right hand operand
- Modulus: Divides left hand operand by right hand operand and returns remainder
- Exponent: Performs exponential (power) calculation on operators
- Floor Division - Division that results in a whole number adjusted to the left in the number line
I was interested in the constants... I understand the whole importing and such to create your own constants, however, I was wondering if they support a library of commonly used constants? Constants such as gravity, pie, etc., do they have a library that simply allows you to import these instead of having to make a new file yourself?
ReplyDeleteYes! In Python, you can import the math library using "import math". This allows you to use common mathematical constants like pi, e, tau, and infinity, as well as math functions like sine, cosine, tangent, and the common logarithm.
DeleteI like what you did with the pokemon names in your example! I use VPython in my Physics Lab course right now and we write constants all the time but not in the same way that you did there. Is there a difference between just calling it the constant and using constant.PI?
ReplyDeleteIf you create the constant pi in a Python file and want to use that constant in the same file, you can simply call the constant. If you want to use that constant in a different file, you have to use the original file name to call the constant. For example: if I have a file named red.py and create a constant called magikarp. If I wanted to call magikarp inside of the red.py file, I would simply type "magikarp". However, if I made another file called blue.py and wanted to call magikarp in that file, I would need to type "red.magikarp".
Delete