Showing posts with label Py_Conditional. Show all posts
Showing posts with label Py_Conditional. Show all posts

Sunday, January 2, 2022

How to print the Fibonacci Series using Python Program

Python Program to print the Fibonacci Series using While Loop
The Fibonacci number series are 0, 1, 1, 2, 3, 5, 8..etc. The logic of the Fibonacci series is as follows:
f0 = 0
f1 = 1
Now the third Fibonacci number 'f' in the series can be obtained by adding the previous two number in the series.
f = f0 + f1

Now we can print the Fibonacci Series up to the user specified range.

n = int(input('How many Fibonacci''s Series to print: '))
f0 = 0
f1 = 1
f = 0

fiblist = [0, 1]

i = 0

if n == 0:

    fiblist = f0

elif n == 1:

    fiblist = f0, f1

else:

    while i < n-2:

        f = f0+f1

        fiblist.append(f)

        f0, f1 = f1, f

        i = i+1

print('The Fibonacci Series between {0} and {1} is: '.format(0, n), fiblist)

# ------------------------------------------------------------------------------------------------------------------------ #
Input:
How many Fibonacci's Series to print: 10

Output:
The Fibonacci Series between 0 and 10 is:  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Process finished with exit code 0

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

How to print the Prime Numbers between 1 to 100 using Python Program

A Python Program to print the Prime Numbers between 1 to 100 using For Loop
import sys
print("Python Version: ", sys.version)

n = 100
j = 0
k = 0
primelist = [ ]

for i in range(1, n + 1):
    for j in range(1, i + 1):
        if i % j == 0:
            k = k + 1
    if k == 2:
        primelist.append(i)
        k = 0
    else:
        k = 0
print('The Prime Numbers between {0} and {1} are: '.format(1, n), primelist)

# ------------------------------------------------------------------------------------------------------------------------ #
Output:
C:\Users\tpreddy\PycharmProjects\pythonProject\PyLab1\venv\Scripts\python.exe C:/Users/tpreddy/PycharmProjects/pythonProject/PyLab1/Temp1.py
Python Version:  3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)]
The Prime Numbers between 1 and 100 are:  [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Process finished with exit code 0

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

How to check a condition with Assert statement in a Python Program

The Assert statement to check whether a condition is true or not in Python Program
Assert:
The 'assert' statement in Python is used to check whether a specified condition is fulfilled or not.
If the condition is False, it will throw an Assertion Error.
Syntax:
assert condition, error message

Example1:
x = int(input('Please enter a number>0: '))
assert x > 0, "Try again! Invalid Input"
print('The number you entered is: ', x)

In the above example, the Python interpreter checks if x>0 is True or not. If it is True, then the next statements will execute, else it will display the AssertionError along with the message we mentioned "Try again! Invalid Input".
# ------------------------------------------------------------------------------------------------------------------------ #
Input : when you entered 0 or -number as input, the output is as follows.
Output:
Traceback (most recent call last):
  File "C:\Userstpreddy\PycharmProjects\pythonProject\PyLab1\Temp1.py", line 6, in <module>
    assert x > 0, "Try again! Invalid Input"
AssertionError: Try again! Invalid Input
# ------------------------------------------------------------------------------------------------------------------------ #
The AssertionError shown in the above output is called an exception. An exception is an error that occurs during the runtime.
To avoid such type of exceptions, we can handle exceptions using the 'try...except' statement.
In the except block, we can write the statements that are executed in case of exception, as shown in the below example:

Example2:
x = int(input('Please enter a number>0: '))
try:
    assert x > 0
    NumValid = True
    print('The number you entered is: ', x)
except AssertionError:
    print("Try again! Invalid Input\n")

# ------------------------------------------------------------------------------------------------------------------------ #
Input : 
Plese enter a number >0: -5

Output:
Traceback (most recent call last):
  File "C:\Users\tpreddy\PycharmProjects\pythonProject\PyLab1\Temp1.py", line 6, in <module>
    assert x > 0, "Try again! Invalid Input"
AssertionError: Try again! Invalid Input

Process finished with exit code 1
# ------------------------------------------------------------------------------------------------------------------------ #

Saturday, January 1, 2022

How to use Break, Continue and Pass statements in Python Program

What are break, continue and pass statements in Python Program
Break: 
The 'break' statement in Python Programming is used inside a For loop or While loop to come out or exit the loop. When 'break' statement is executed, the Python interpreter jumps out of the loop to process the next statements block in the program.

Continue: 
The 'continue' statement in Python Programming is used inside a For loop or While loop to skip the further execution of the current iteration and go back to the next iteration of the loop. When the 'continue' statement is executed, the next statements in the same loop are not executed further.

Pass: 
The 'pass' statement in Python Programming will not do any specific operation. It will be used in the Loops or If conditions to represent no operation. However we use the 'pass' statement when we need a statement syntactically but do not to perform any specific operation.

Example:
We can understand the usage of the above 3 statements from the following program.
# A Python program to print Even and Odd numbers between 0 to 15
import sys
print("Python Version: ", sys.version)

x = 0
while x <= 30:
    x = x + 1
    if (x-1) % 2 == 0:
        print('The even number is = ', x-1)
        continue  # Move back to next iteration
    else:
        pass  # Continue to next steps
    if x > 15:
        break  # Exit the loop
    print('The odd number is = ', x-1)
print('The loop is exited after {0} iterations'.format(x-1))

# ------------------------------------------------------------------------------------------------------------------------ #
Output:
C:\Users\tpreddy\PycharmProjects\pythonProject\PyLab1\venv\Scripts\python.exe C:/Users/tpreddy/PycharmProjects/pythonProject/PyLab1/Temp1.py
Python Version:  3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)]
The even number is =  0
The odd number is =  1
The even number is =  2
The odd number is =  3
The even number is =  4
The odd number is =  5
The even number is =  6
The odd number is =  7
The even number is =  8
The odd number is =  9
The even number is =  10
The odd number is =  11
The even number is =  12
The odd number is =  13
The even number is =  14
The loop is exited after 15 iterations

Process finished with exit code 0

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

How to display numbers from 1 to 100 in 10 rows and 10 columns using Nested For Loop in Python Program

Python Program to display the numbers from 1 to 100 in 10 rows and 10 cols

for x in range(1, 11):

    for y in range(1, 11):

        print('{:8}'.format(x * y), end='')

    print()

# ------------------------------------------------------------------------------------------------------------------------ #
Output:


       1       2       3       4       5       6       7       8       9      10

       2       4       6       8      10      12      14      16      18      20

       3       6       9      12      15      18      21      24      27      30

       4       8      12      16      20      24      28      32      36      40

       5      10      15      20      25      30      35      40      45      50

       6      12      18      24      30      36      42      48      54      60

       7      14      21      28      35      42      49      56      63      70

       8      16      24      32      40      48      56      64      72      80

       9      18      27      36      45      54      63      72      81      90

      10      20      30      40      50      60      70      80      90     100

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

How to check whether an element exists in a list using For Loop in Python Program

The Python Program to search for an element in a List
# import sys
# print("Python Version: ", sys.version)

L1 = {3, 5, 'Vijay', 'James Bond', '007', 15}

search_element = input("Please enter an element to search: ")

for element in L1:

    if search_element == '%s' % (element):

        print("The element found in the list")

        break

else:

    print("The element not found in the list")

# ------------------------------------------------------------------------------------------------------------------------ #
Input: 
007

Output :
The element found in the list

#--------------------------------------------------------------Thanks--------------------------------------------------------------#


How to display the stars in Triangular form using For Loop in Python Program

The Python Program to display the stars in Triangular Form
# import sys
# print("Python Version: ", sys.version)

n = 40

for i in range(1, 11):

    print(' ' * n, end='')

    print('*  ' * i)

    n = n - 1

# the simplified version of the above code:

for i in range(1, 11):

    print(' ' * (n - i) + '*  ' * i)

# ------------------------------------------------------------------------------------------------------------------------ #

Output :

                        *  

                                       *  *  

                                      *  *  *  

                                     *  *  *  *  

                                    *  *  *  *  *  

                                   *  *  *  *  *  *  

                                  *  *  *  *  *  *  *  

                                 *  *  *  *  *  *  *  *  

                                *  *  *  *  *  *  *  *  *  

                               *  *  *  *  *  *  *  *  *  *

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose we have a sample Sales data, which is stored with Monthly Snapsh...

Popular Posts from this Blog