In this lesson, we will discuss basic elements in programming, with a focus on the following key points:
- What are
variables
, and how are they used in a program? - Learn to work with math operations in an introductory programming language; Python.
This is a general purpose tutorial for multiple languages. For the Javascript-specific version of this lesson that focuses on fundamentals frontend engineers need to know, please click here.
The act of storing information in programming languages is important. Often during complex calculations, we need to reference some saved
information later. In programming languages, this storing of information is done using variables
. In this lesson, we will discuss how variables
are used, and some basic math operations
that can be performed in a program.
Variables
Variables can store and manipulate data in a programming language. Data values can be assigned to a variable by using the assignment operator
which is the equal sign
(=). Once a value is assigned to the variable, it corresponds to that value unless it is reassigned. Let's see some code examples below.
In Python, variables can be directly initialized using a variable name and assigning it a value. The code block below shows the creation of the variable number
which has the value 12
.
1int number = 12;
2
3cout << number; // prints 12
If this code runs on a Python interpreter, the value that you stored earlier in number
is successfully displayed.
Let's test your variable knowledge with some short questions!
Let's test your knowledge. Is this statement true or false?
Is the following code snippet a valid way to declare a variable in JavaScript?
1int num12 = 12;
Press true if you believe the statement is correct, or false otherwise.
Try this exercise. Click the correct answer from the options.
What is the value stored in variable c
?
1int a = 10;
2int b = a;
3int c = b;
Click the option that best answers the question.
- 20
- 0
- 10
- Gives an error
Basic Math Operations
In any programming language, one of the very basic tasks is to perform various mathematical calculations. These calculations can be performed by using the arithmetic operators that we have in basic maths! Using these operators combined with some other different aspects of the programming language can produce interesting results. A list of some common mathematical operators in programming languages is given below. The examples assume that the values of a
and b
are equal to 10.
Operator | Operation | Example |
---|---|---|
+ | Addition | a + b = 20 |
- | Subtraction | a - b = 10 |
* | Multiplication | a * b = 100 |
/ | Division | a / b = 1 |
% | Modulus | a % b = 0 |
** | Exponent | a ** b = 1 x 10^10 |
Let's see these operations in action by looking at some code examples in Python.
Math in Python
In Python, mathematical operations can be performed by directly specifying a mathematical statement. If you're using any variables in the calculation, make sure to initialize them beforehand.
These values can also be saved in further variables, which is also shown in the codeblock below.
xxxxxxxxxx
int main() {
int a = 13;
int b = 45;
std::cout << a + b << std::endl; // prints 58
std::cout << b - a << std::endl; // prints 32
std::cout << a * b << std::endl; // prints 585
std::cout << b % a << std::endl; // prints 6
int c = a + b;
std::cout << c << std::endl; // prints 58
return 0;
}
This is helpful in situations where you need to do more complex calculations, which may require multiple steps.
Python also has some specific operations which may be defined differently in other languages. One such operation is the floor division
operator (denoted by //
). This operation performs division between two numbers and gives the quotient in which the digits after the decimal point are removed. For example:
xxxxxxxxxx
int a = 5;
int b = 8;
std::cout << b / a; // prints 1
You must be wondering by now, are these the only mathematical operations supported by Python? The answer is no. Python supports a host of other operations, however, they are not provided using simple operators. These can be accessed through the Python math library. Simply put an import
statement at the start of the program, and you can access more maths operators from there such as square root, floor and ceiling functions.
1#include<cmath>
2#include<iostream>
3
4int a = 100;
5std::cout << sqrt(a); // prints 10 (square root of 100)
A comprehensive list of all operations provided by the Python math library can be read from their documentation here.
Try this exercise. Click the correct answer from the options.
What will be the output of the following code block in JavaScript?
1#include <cmath>
2#include <iostream>
3
4int a = 10;
5int b = 36;
6double c = sqrt(b) + a;
7std::cout << c << std::endl;
Click the option that best answers the question.
- 6
- 16
- 36
- 30
Build your intuition. Fill in the missing part by typing it in.
What will be the output of the following code block in Python?
1int a = 8;
2int b = 12;
3int c = b / a + a;
4cout << c;
Write the missing line below.
Summary
In this lesson, basic programming concepts about variables and mathematical operators were introduced. The most important thing about working with them is to ensure that there are no syntax errors. Mastering these concepts is important as they will be the building blocks to the next set of techniques in programming!
One Pager Cheat Sheet
- This lesson covers the use of
variables
to store information, and the basics ofmath operations
in programming languages. - Variables are used to
store and manipulate data
in a programming language by using theassignment operator
(equal sign
(=)) toassign values
to the variable. - Variables can be directly initialized as
number
with value12
on both Python and JavaScript interpreters. - No valid JavaScript variable name must start with a letter, underscore (_), or dollar sign ($) and
follow camelCase naming convention
for proper syntax. - The value stored in
variable c
is equal to 10. - Programming languages allow us to perform various mathematical operations using basic arithmetic operators such as addition, subtraction, multiplication, division, modulus and exponentiation.
- You can
directly
perform math operations in Python, and save the result in new variables if needed. - Python is a powerful tool which offers rich functionality, including specific operations such as
floor division
(//
), which can be used to perform complex calculations. - In Python, additional mathematical operations not available with simple operators
can be accessed
through the Python math library by including animport
statement into the program. - The
Math.sqrt()
function is used to calculate the square root of 36 and added to 10, producing an output of 16. - The output of the
console.log()
in JavaScript is9
due to--c
being a decrement operator after variablesa
andb
were both assigned values. - Mastering basic programming concepts such as
variables
andmathematical operators
is important as they are the building blocks for further programming techniques.