Python Basics




Arithmetic Operations

We can perform simple Arithmetic Operations Using Python, As shown below if we Type "2+3+5" and press Shift+Enter then the Result will be Printed out on to screen

2+3+5





Like Wise we can do ADDITION, MULTIPLICATION, SUBSTRACTION, AND DIVISION Using PYTHON.






Using the "//" Operation Will Give us the Quotient Part While performing Division





The Percentage or the Modulo Operation "%" Will give us the Remainder as an Output


String Operations

We can also use the Addition Operator to Concatenate Strings




Now if we try to add a number to the Above Strings ("Hi" + "I am Python" + 3)

then we will get the error



If we put the Integer 3 in Double Quotes then it can be Concatenated with other strings




However, if we multiply a String by 3 then the String will be printed Three Times



If we try to multiply a String by a String ("Hi" * "I am Python")

then Python will throw an error


Boolean Operations

In Python, The boolean Value "True" has a Value of "1", So adding the

Boolean Value 3 Times (True + True + True) will give an output of "3"




Likewise a Boolean Value "False" has a Value of "0" Assigned to it,
So adding a Value of False 3 Times (False + False +False) will give an Output of "0"







Below are a few more examples of Arithmetic operations on Boolean Operators



Logical Operations


We can check for logical operations in Python, The logical expression (4 < 5)
Will evaluate to TRUE since 4 is less than 5, and the logical operation (4 > 5)
Will evaluate to False since 4 is not greater than 5





We can also use "Less Than or Equal to" (4 <= 5) or "Greater Than or Equal to" (4 >= 5)
To Evaluate a Condition.





If we try to compare Two Numbers using an " = " (Equal to Operator)
Then we will encounter an error. " = " Equal to is an ASSIGNMENT Operator
, not a COMPARISON Operator.




We can use a "==" Double Equal to the operator for comparison




Below is another example where we are checking the EQUALITY of two Boolean Operators
"True = True" will give an error while "True == True" will performing Equality Check
without any issues.





We can also use the "AND", and "OR" Operators to validate a condition, for instance in the below example both of the conditions need to be true in order for the condition to PASS, here Both the conditions "2 < 4" and "4 > 2" are True so the output will be TRUE


Likewise, if any one of the conditions is NOT TRUE the output will be FALSE


We can also use the Logical "OR" Condition, In the below example One of the Condition "2 < 4" Is correct while "4 < 2" is Incorrect, but since we are using an "OR" Condition even if one of the condition is "TRUE" the Result will be "TRUE"


Using the "NOT" operator will Flip the result Set, Here "1 > 2" is False, but if we Add a "NOT " Operator in front of "1 > 2" Then the Output will Flip to "TRUE"




VARIABLES in Python

We can also assign a Variable to the Output, For example, if we create a variable "total" and assign the output of (2 + 3 + 5) to it the the Variable total will hold the value of that resultset.

total= 2+3+5
print (total)


Likewise as shown below we have created Three variables "length", "Breadth" and "Area", 
We have assigned a value of 100 to both Length & Breadth and then we are using the variable "AREA" to hold the value of Length & Breadth


length = 100
breadth = 100
area = length * breadth
print(area)


Below is another useful example, here Writing a syntax like "var = var * 2" is equal 
to "var *= var" these two syntaxes will produce the same output.


TYPE CONVERSION


We can use the "type" Function to check the type of a variable it depends on the type of value we put in the variable.


Below are a few examples where we are checking the types of Lists, Tuples, and Dictionaries




We can print all the Lists, tuples, and Dictionaries in a single Print Statement as shown below.



For Strings, we can either use "Single Quotes", "Double Quotes" & "Triple Quotes"


Look Closely at the first statement, What if we are using an apostrophe in the String with Single Quotes, in this case, we will get an error, To Rectify these kinds of issues we can use the Double Quotes instead of Single Quotes.



However, we can use the escape Sequence " \ " character if we need to use the apostrophe after s.




So now the question is why do we need Triple Quotes  for Strings, The answer is, IF we need to add a multiline  String we will get an error with Single and Double Quotes, to Print Multiline Strings we can use Triple Quotes 


IF, ELSE & ELIF


Below is a Simple Program to Showcase the functionality of IF, ELSE, and ELIF statements in Python


In the Below example, the speed of the car is 120 which is in (Line1), We will use the IF condition to check if the speed of the car is below or above the speed limit, (Line2), In this case, the speed of the car is more than 80 so the compiler will print out the statements in (Line3 & Line4)


speed = 120
if speed > 80 :
print('You are too fast , Please Slow Down')
print('You might get a ticket for speeding')


In the below case, However, the speed of the car is less than 80 and we have an IF condition in (Line2) to check that, so once the condition satisfies the Print statements in (Line3&4) will be printed out.


Please Note the last Print Statement on (Line5) will print out whether the if condition is True or False because it’s not in the scope of the IF statement,


Python uses line Indentation to define the scope of the IF Statement, The Spaces in front of (Line3&4) define that (Line3&4) is in the scope of the IF statement while (Line4) is not in the scope of the IF statement.



speed = 70
if speed < 80 :
print('You are in Your Speed Limits')
print('Please enjoy your drive')
print('Have a Nice day !')


Let’s combine the above two conditions into one, where we can make use of ELIF statements, It’s like saying if speed is Greater than 80 do This, if the speed is Less than 80 do this else print ‘Have a nice day



speed = 70
if speed > 80 :
print('You are too fast , Please Slow Down')
print('You might get a ticket for speeding')
elif speed <80 :
print('You are in Your Speed Limits')
print('Please enjoy your drive')
else:
print('Have a Nice day !')


Let's see another example where we can make use of the AND /OR operator to enhance the functionality of Python



speed = 70
if speed < 70 and speed > 50:
print('You should speed up a little !!')
elif speed < 80 and speed > 70:
print('You are driving efficiently')
elif speed > 80 and speed < 120:
print('You are at a High Rated Speed , Please Slow Down.')
else:
print('Have a Nice day !')

Post a Comment

0 Comments