Red Security

Full Version: Python 3: Basic Operations and variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Some Basic Operations:

     In this tutorial we will be covering Variables, arithmetic operations, Data types and also some other math stuff. If you don’t love math don’t worry you don’t need to be a master in math to learn a language because if you want to do any mathematical operation you just give commands to the computer and it is done. It is so easy. In previous threads we saw how we can print strings on screen before we dive into operations. Let's see how we can print numbers on the screen..

Code:
print(100)


So that’s it so simple, just write it without quotations then it will be treated as an integer. And it will be printed on the screen.

Let’s do something more. Let’s add some numbers, don't worry we will give commands and it will be done..

Code:
print(10+20+30)


Isn't it simple? We can do the same with other operations. Let's check it out.

Code:
print(30-20-10)
print(3*2*5)
print(10/5)

Now what if we want to print the remainder of a division? Let’s write a code to get a better understanding of it..


Code:
print(10%5)
print(10%3)

Output: 0
        1


So it will print the remainder of the division, and the percentage sign is called modulo operator in programming.

Note:  Keep Practicing by changing the values in the print function..

If you practice enough then we can dive into some other cool stuff which is exponentiation don’t be worried about the word `exponentiation`. It means taking the power of a number first to understand in mathematics how it works. If we say 2 power 5, it means we are multiplying 2 with itself 5 times just like

2*2*2*2*2 = 32

Let's code it..

Code:
print (2**5)


This is it for exponentiation we use two asterisks between the numbers.
That’s it for the mathematics in Python. Let’s see some other concepts

Variable:
    If we want to store our above value resulted by the mathematical operation we use variables. variables store the data and whenever we want that data we just call the variable. variables store data in different type i.e Integers, Strings, floats (explained below)

Syntax of declaring a variable is so simple

Code:
n = 10

print(n)

let's store some string inside a variable and print it out 
Code:
name = "Covid-19"

print(name)

for float numbers the same rule applies

Now in this code it will print the value stored in the variable named ‘n’ python is smart enough to differentiate between the data. Data types are,,,
Integers (It means numbers positive and negative both. Like 123, -68, 32)
String (Anything between quotations in python is called string. Like “World” , “US” , “123”)
Float (These are the numbers with decimal points. Like 123.53 , 4.0 , 32.8)
If you divide two numbers the python will automatically generate the resultant as float even if it is a whole number. Let’s see an example
Code:
result  =  20/5
print(result)

Output: 4.0


Exclamation The resultant of a division is always a floating number..

Idea Task to do: Google the naming rules of a variable in Python.


("Allah Hafiz" and "Good Bye" Till the next post )
"Keep Learning and Keep exploring"
Thank you for this amazing tutorial.. keep it up.