Saturday, May 30, 2020

Iterations – While Loop


Consider the given below program.



















Here we already assigned the value -999999999 to the variable largest_number. In this program, if we input a number that not equal to -1 then the program will check the entered number is greater than or less than to the given largest number.

If the entered number is greater than the given number, then the value of “largest_number” is updated to the entered number. This process will continue until we entered the -1.

If we entered the -1, then the program will be terminated.

Given below the snapshot of one of the tested output.

















Let’s consider the below program that wrote to count the number of even numbers & odd numbers that entered by user.


























Given below the snapshot of one of the tested output.
















Note:

Given below the two types of forms are equivalent.

While number ! = 0: and while number:

If number % 2 == 1 : and if number % 2 :



Using a counter variable to exit a loop







The output is given below.











A junior magician has picked a secret number. He has hidden it in a variable named “secret_number”. He wants everyone who run his program to play the Guess the secret number game, and guess what number he has picked for them. Those who don't guess the number will be stuck in an endless loop forever!

Given below the code for the above scenario.












One tested output is given below.


Conditional Statements


Given below the program to find the largest number from given two numbers.











Output is







We can use the above program as below. But the output is same.










This style, however, may be misleading, and we're not going to use it in our future programs, but it's definitely worth knowing if you want to read and understand someone else's programs.

Given below the program to find the largest number from given three numbers.














Output is








Try this program.













Output is





Friday, May 29, 2020

Comparison Operators


Equality Operator


This operator also known as equal to operator. (==)

This is a binary operator with left-sided binding. It needs two arguments & checks if they are equal.









Inequality Operator


This operator also known as not equal to operator. (!=)









Greater than / Greater than or equal to / Less than / Less than or equal to




Write a simple two-line program that takes the parameter “n” as input, which is an integer, and prints “False” if “n” is less than 100, and “True” if “n” is greater than or equal to 100.





Here,


If we input n=55 then output is False

If we input n=99 then output is False

If we input n=100 then output is True

If we input n=101 then output is True

If we input n=-5 then output is False

If we input n=+123 then output is True

Type Conversion


str () Function


Let’s consider again in “right-angle triangle” program.






Output is,







Here we can pass the whole result to the print () function as one string by forgetting about the commas.

String Operators


Concatenation



The Plus (+) sign, when applied to two strings becomes a concatenation operator.

If we want the Plus (+) sign to be a concatenate not an adder, we must ensure that both its arguments are strings.








Output will be like,









Using + to concatenate strings lets we construct the output in a more precise way than with a pure print () function. Even if enriched with the end= and sep= keyword arguments.


Replication


The Asterisk (*) sign, when applied to a string & a number becomes a replication operator.

This operator replicates the string, the same number of times specified by the number.

A number less than or equal to zero produces an empty string.










Let's draw a triangle.






So, the output is









Type Casting


Python offers two simple functions to specify a type of data and solve this problem - here they are: int () and float ().

Their names are self-commenting.

The int () function takes one argument & tries to convert it into an integer; if it fails, the whole program will fail too.
The float () function takes one argument & tries to convert it into a float; if it fails, the whole program will fail too.

Let’s run the following code.






Output is,






We will consider next code. This is known as “right-angle triangle” program.


Output is,



Thursday, May 28, 2020

The input () function


With an Argument






The input () function is invoked with one argument. It’s a string containing a message.

The message will be displayed on the console before the user is given an opportunity to enter anything.

Input () will then do its job.

The result of the input () function is a string. It’s not an integer or a float.

So, we mustn’t use it as an argument of any arithmetic operation.



Let’s run the following code.








We get the following error.


Solve an Algebraic Equation


When we code the above program,








If we input x = 0 we can get output as y = - 1.0

If we input x = 1 we can get output as y = 3.0

If we input x = -1 we can get output as y = - 9.0


Let’s consider about another equation.


When we code the above program,







If we input x = 1 we can get output as y = 0.6000000000000001

If we input x = 10 we can get output as y = 0.09901951266867294

If we input x = 100 we can get output as y = 0.009999000199950014

If we input x = -5 we can get output as y = -0.19258202567760344

Miles to Kilo Meters Conversion Program


Miles and kilometres are units of length or distance.

Bearing in mind that 1 mile is equal to approximately 1.61 kilometres.














Output is










Shortcut Operators


Let’s say “op” is a two-argument operator & the operator is used in the following context.

variable = variable op expression

It can be simplified & shown as follows.

variable op= expression

Given below the examples.




Solve a mathematical problem in Pythagorean Theorem.


The square of the hypotenuse is equal to the sum of the squares of the other two sides.





Assume that A = 4.0 & B = 3.0

Let’s find the value of C.

Given below the program to find the value of C.








Output is 5.0

Variables


If we want to give a name to a variable, there’re some rules to follow.

1.       Variable name must be composed of upper case letters, lower case letters, digits and characters (underscore).

2.       Variable name must begin with a letter.

3.       Underscore character is also a letter.

4.       Variables with same meaning but, with different upper case & lower case letters are treated as two different variables.

Example:

THETEXT
thetext
theText

Given above three different variables.

5.       Variable name must not be any of Python’s reserved word.

NOTE

The PEP 8 – Style Guide for Python Code recommends the following naming convention for variables & functions in Python.

1.       Variable names should be lowercase with words separated by underscores to improve readability.

Example: my_variable

2.       Function names follow the same convention as variable names.

Example: my_function

3.       It’s also possible to use mixed case, but only in contexts where that’s already the prevailing style, to retain backwards compatibility with the adopted convention.

Example: myVariable


Keywords

Given below the reserved keywords in Python. They are reserved because we mustn’t use them as names, neither for the variables, nor functions. The meaning of the reserved word is predefined, and mustn’t changed any way.

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']


A variable comes into existence as a result of assigning a value to it. Unlike in other languages, we don’t need to declare it in any special way. If you assign any value to a non-existent variable, the variable will be automatically created. We don’t need to do anything else.

The creation is very simple, just use the name of the desired variable then the equal sign (=) and the value you want to put into the variable.

Sunday, May 24, 2020

Binding of the operators


The binding of the operator determines the order of computations performed by some operators with equal priority, put side by side in one expression.

In Python, most of the operators have left-sided binding. That means the calculation of the expression is conducted from left to right.





In correct way, from left to right:

9 % 6 gives 3 and then 3 % 2 gives 1
In incorrect way, from right to left:
6 % 2 gives 0 and then 9 % 0 causes a fatal error.

In Python, modulo operator has left-sided binding.







In correct way, from right to left:

2 ** 3 gives 8 and then 2 ** 8 gives 256
In incorrect way, from left to right:
2 ** 2 gives 4 and then 4 ** 3 gives 64

In Python, exponentiation operator has right-sided binding.


Operator Priorities


Example:






Parentheses with operators


According to the arithmetic rules, sub-expressions in parentheses are always calculated first.


Arithmetic Operators – Part II


Remainder (Modulo)

Percent sign (%) is the remainder operator. In other programming languages it also known as modulo.

The result of the operator is a remainder left after the integer division.





Reasons for the above result:

14 // 4 gives 3 (integer quotient)
3 * 4 gives 12 (quotient & divisor multiplication)
14 – 12 gives 2 (remainder)

When we consider the below example,



The answer is not 3 but 3.0

Reasons for the above result:
12 // 4.5 gives 2.0
2.0 * 4.5 gives 9.0
12 – 9.0 gives 3.0

Addition

Plus (+) sign is the addition operator.








Subtraction / Unary & Binary Operators


Minus (-) sign is the subtraction operator. But we should notice this this symbol can change the sign of a number.

So, we have to say here there is an important distinction between unary & binary operators.


In subtracting scenarios, the minus operator expects two arguments.


From the above reason, the subtraction operator is considered to be one of the binary operators.

But the minus operator may be used in a different (unary) way.








There is also a unary (+) operator.





The operator preserves the sign of its only argument.