Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 3: Basic Operations and variables

1 Replies, 850 Views

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"
Love me like you do Heart
(This post was last modified: 04-17-2022, 12:52 AM by Covid-19.)
Thank you for this amazing tutorial.. keep it up.
Rs
* Thankful to Allah *
Kurdy

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,249 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,639 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,675 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,844 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,608 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,842 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,793 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,833 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,826 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,828 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)