Monday, November 9, 2020

Conditional Statement if, if and else, if and elif with else using Arithmetic Operators and Logical Operators in python3

 If you are new to coding means conditional statement will be unfamiliar to you. For that we can take a real-time example for understanding it, for example if we need to check a condition, like above 50% is the pass mark means we need to put a condition for that to check whether mark is above 50 or not, so for that we need the if statement in python to write the condition. The code should be stated as below


if mark > 50:
print("You are pass")

Here ‘if’ is the keyword to check the condition and ‘mark’ is the variable which carries the input of the student’s mark, as per the above condition we are checking whether the student’s mark is greater than pass mark(50) or not. 

We can see a flow chart




I think it should be clear, if the condition satisfies means only "a is greater than b" will be printed 

Now we can see the whole program so that it will be clear.

mark = input('Enter the mark:')
mark = float(mark)
if mark > 50:
print('You are pass')

mark variable is getting input from the user and then we are converting that value into float value and then we are checking whether it is greater than 50 or not, if yes means the statement 'You are pass' will be printed.


Next we can see another scenario

now assume that if mark is not greater than 50 means we need to print a message that ‘you are fail’, in this situation we have to put else keyword, let’s see the code

 

mark = input ('Enter the mark:')
mark = float(mark)
if mark > 50:
print('You are pass')
else:
print('You are fail')

Then another important thing is indent a tab can be seen in the next line of the if statement, this is used to mention that those statements are under the if block, likewise for if statement what indent is given should be the same for else block also, indent spacing is very important in python, as in java we will use braces and all, instead of that only indent is used in python, this will make sure the blocks. For example if mark is greater than 50 means we may want to display some three or four statements, so how it will be understandable, it is through indent only, see the below code

 

 if mark > 50:
print('Congrates')
print('You are pass')
print('The mark you got is:',mark)
else:
print('You are fail')

So as per the coding the three print statements will be coming under the ‘if’ block, if the ‘if condition’ is true means, these three lines will be printed.


Now we can see where we want to use elif, let we assume that we want to print the below statements for the appropriate scenarios means

“You are pass” for mark above than 50

“You got distinction” for mark above than 75 

“You are fail” for mark less than 51


if mark > 50 and mark <= 75:
print('You are pass')
elif mark > 75;
print('You got distinction')
else:
print('You are fail')

For better understanding, I will write the values for what mark we will get what message

If the mark is between 50 and 76 (ie., 51 to 75) means message "You are pass" will be printed

If the mark is above 75 means message "You got distinction" will be printed

If mark is less than 51 means message "You are fail" will be printed


Likewise it is not compulsory to have else statement on every if or elif statements, as per the need we can use, likewise if needed we can have as many as elif we needed like


if mark > 50 and mark <= 75:
print('You are pass')
elif mark > 75 and mark < 100:
print('You got distinction')
elif mark > 100 or mark < 0:
print('Please enter valid mark from 0 to 100')
else:
print('You are fail')

Ok, now we can see what are the conditions we can use
Double Equals == 
Not Equals != 
Less than < 
Less than or equal to <= 
Greater than > 
Greater than or equal to >=

Finally we can see a flow chart for if elif and else



Now we can see Arithmetic Operators in python

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

** Exponentiation

// Floor division



Then the Logical Operators

and

or

not


You had seen the above mentioned logical operators in the condition we had used the sample program


If you are having doubts still means you can view the below video




Sunday, August 9, 2020

Using int, float and calculating them by casting the type and Operator Precedence in python3

In this post we are going to see, how to use the datatype int and float to do calculations. First we can see how to initialize values to a int or float variable

a = 50
b = 40.9

now we assigned two values to two variables
where for "a", int value is assigned, so the datatype for "a" will be int and for "b" float value is assigned, so the variable "b" will be float variable

you can check the type of the variable using 'type' keyword

>>> a = 50
>>> b = 40.9
>>> type(a)
<class 'int'>
>>> type(b)
<class 'float'>


Next we can see about casting, 

Code:
"""
Numeric Types: int, float castings

"""
import random

d = random.randrange(1,10)

print("type(d):",type(d))
print("d:", d)  
d = float(d)
print("type(d):",type(d))
print("d:", d)  
d = d +8.9
print("type(d):",type(d))
print("d:", d)
d = int(d)
print("type(d):",type(d))
print("d:", d)


the output
type(d): <class 'int'>
d: 4
type(d): <class 'float'>
d: 4.0
type(d): <class 'float'>
d: 12.9
type(d): <class 'int'>
d: 12


using random to get a number in the range of 1 to 10 to get assigned to the variable d, so it is a int variable and the value will be in integer

type(d): <class 'int'>
d: 4

Then we used float(d) get assigned to d, so here the d variable will be changed to float datatype and you can see the value is changed to float like 4.0

type(d): <class 'float'>
d: 4.0

and to change back to int we can use the int(value) method, to cast back to int.

Next we can see about a simple multiplication,

Code:
"""
Numeric Types: int, float
multiplying a int a float will result float
"""
a = 50
b = 40.9
res = a * b
print("type(a):",type(a))
print("type(b):",type(b))
print("type(res):",type(res))
print("a * b:", res)



here we are multiplying a int and a float value and assigned to a variable, and result will be in float and the variable's type will be float.

the output
type(a): <class 'int'>
type(b): <class 'float'>
type(res): <class 'float'>
a * b: 2045.0

Now we can see another program for division

Code:
"""
Numeric Types: int, float
Divide a int with float will result float
If result of the divide value is float means the result will be in float even that the variables are int
"""
a = 50
b = 40.9
c = 7
res = a / b
resInt = a/c
print("type(a):",type(a))
print("type(b):",type(b))
print("type(res):",type(res))
print("type(resInt):",type(res))
print("a / b:", res, "rounded to 4 decimal:", round(res,4))
print("a / c:", resInt)



the output
type(a): <class 'int'>
type(b): <class 'float'>
type(res): <class 'float'>
type(resInt): <class 'float'>
a / b: 1.2224938875305624 rounded to 4 decimal: 1.2225
a / c: 7.142857142857143


here you can see the output of dividing a / b has more values after the decimal, so if we need only 5 values after the decimal point means we can use round method as shown in the above code, while using this if the sixth value is more than 4 means the 5th place value will be increased by 1.

We can see another example for calculation

Code:
"""
Numeric Types: int, float
Calculations
"""
a = 80
b = 4
c = 9
addition = a + b
subtraction = addition - c
divide = a / b
multiply = b * c
calculation = a + b / 2 - (a / b)
print ("addition:",addition)
print ("subtraction:",subtraction)
print ("divide:",divide)
print ("multiply:",multiply)
print ("calculation:",calculation)


the output
addition: 84
subtraction: 75
divide: 20.0
multiply: 36
calculation: 62.0


here addition, subtraction, division and multiply is normal things, but we are having a code

calculation = a + b / 2 - (a / b)

here we can see how the calculation done here step by step

calculation = a + b / 2 - (a / b)
we can write the equalent value of the variable
calculation = 80 + 4 / 2 - (80 / 4)
the calcuation will be done from left to right
and the order is as followed 

Operator Precedence
1 P Parentheses
2 E Exponent
3 M Multiplication
4 D Division
5 A Addition
6 S Subtraction


so we can see the steps to get the result

calculation = 80 + 4 / 2 - (80 / 4)
calculation = 80 + 4 / 2 - 20
calculation = 80 + 2 - 20
calculation = 82 - 20
calculation = 62

You can see the below video for better understanding of this post




In next post we can see about using strings

Wednesday, July 15, 2020

Python code: Get input from user and print it with concatenating a string and give comments

Write a program inside the file(with .py as extension) to get input from user and display in screen
Code:
"""
Simple program to get input
to display string with input we got
giving comments
"""
design='---------------------------------------'
info = input("Enter your name:")
print(design + "start" + design)
print('Your name is:', info) #This will print 'Your name is: <<Entered name>>'
print(design , "end" , design)
Were in  this above code, it is explained to

  • write comments, for multiple line comments, we need to start and end with triple double quotes(""")
  • to write single line comments you can start with hash (#)
  • you can assign a string value to variable, like
    • design='---------------------------------------'
    • for assigning string value you can use single quotes or double quotes on both sides as mentioned above
  • to print something we need to use print function
    • print function is a build-in function in python to print values
    • We can directly print string values using single quotes or double quotes
      • print("Welcome")
      • print('Welcome')
    • Concatenate string with a variable
      • Assume that string John is stored on a variable called variablename now if we need to print Welcome John means
        • print("Welcome",variablename) #Output will be Welcome John, an automatic space will be there if we use comma
        • if we don't need space means print("Welcome"+John)
      • While concatenating using +
        • string + string #correct
        • string + int #wrong
        • int + int #correct
    • Getting input
      • We need to use input() function to get input from user
You can see the below video for better understanding of this post



In next post we can see about using numbers and calculations

Sunday, July 12, 2020

Run a simple python code in windows machine

Run python in windows machine

You can download python3 for windows from the below mentioned site



Download and install it, then open the command prompt and type python


After  typing python and clicking enter, you will get into the python shell.

Now you can write a print statement to run a basic python program after ">>>"




This line is used to print the message "This is the first python program"

print is a function in python to display message. the message can be inside single quotes or double quotes

>>> print('This is the first python program')

                          (OR)

>>> print("This is the first python program")

both are correct

In next post we can see about getting input and printing that with a static message. 

Conditional Statement if, if and else, if and elif with else using Arithmetic Operators and Logical Operators in python3

 If you are new to coding means conditional statement will be unfamiliar to you. For that we can take a real-time example for understanding ...