Wednesday, December 1, 2021

Basic Programming in Python 3

Whitespace - Indentation

At this point, you may be wondering how Python knows which statements get grouped for if the statements while loops and function definitions and languages like C++ C and Java they use braces Python does not have any syntax for braces in MATLAB, you have the end a statement that indicates the end of a block of code in Python all of this is determined by indentation let's take a look at a couple of examples let's say X is some value in this case we see that the indentation puts the print high a statement within the code block so executing this code no matter what the value of x is will always print hello but only if X is true we'll high get printed so let's look at a slightly more the complicated example we again have a print high and then we have print hello and then aligned with this we have a print high again, in this case, these two are not aligned properly so this is maybe two spaces and this is one space here and this will cause an error in Python everything has to be aligned within a given code block now this syntax is okay and we get the same type of indentation for code blocks with functions and while loops everything is just denoted by indentation.

Read, Evaluate, Print, Loop

Hello everyone, in this article we will look at the Repple in Python. so, what is ripple stand for ripple is basically read evaluate print and then go back again to read so what does it mean it means that the shell environment in Python is an interactive environment and whatever we type into Python and when we hit enter it reads that then it evaluates whatever we have said that needs to be done and then it prints back the results to us and then it loops back to the read mode where it waits for us to give more input to Python so that it can again evaluate and print so it goes in this roundabout fashion where it reads evaluates prints and then again goes back to read so you can also go ahead and read this on Wikipedia I will leave a note for this in the in the video notes so I'll leave the hyperlink for this and if you want you can further read this let's just go and play of it with Python to understand what ripple is so let's type Python so this repple have started so this is the first print where it tells us the version and what what version of python we are using so for example if we type 5 plus 3 and hit enter python reads that evaluates it and then prints the feedback sorry prints the result said to us and then loops back to the read mode again so if i type 5 in to say 6 it has again read it evaluated at printed and looped back to the read state we can also assign variables like y is equal to 8 and that stays in the memory it has read it and then we can say why into five where it will evenly evaluate the result of fine to five and print it back to us like 40 we can also use underscore underscore basically helps his point to the last variable assignment and I can say into five it will give us four oh so 40 was the last result set and I have be used it and if I do again in to underscore into five it becomes a thousand set underscore basically refers to the last results that we've had or the last print and we can reuse that something which is an anomaly here is for example the print statement because there is nothing to evaluate if I say hello world and hit enter it just prints so in this case it reads there is nothing to evaluate but it prints and then it loops back to the read now he will just quit Python here and move back to our command prompt so I'm in Windows I will just type control Z and then hit enter and it brings me back to my Windows prompt so in this video we learned about the repel or how Python works interactively in a shell environment I hope you find this video useful and thanks for watching this video please do subscribe to my channel if you enjoy what I am doing Thanks.

Hands-on - Practice Question PYTHON 3

1. Print

Greeting Quote

Mr Greet is the name of the digital notice board placed at the entrance of the seminar hall. Its purpose is to welcome each and every participant of the seminar by showing a welcoming quote with their name on it.

It is based on the function ‘Greet’ which takes a single parameter ‘Name’ as a String are the names of the participants as Input One by one.

Write the function definition for ‘Greet’ which will generate the welcoming quote as below :

For Example, the ‘Name’ is “Ramakrishnan” then the welcoming quote will be :

Output:

Welcome, Ramakrishnan.

It is our pleasure to invite you.

Have a wonderful day.

 

Note:

Name’ must be of String Datatype.

 

Input Format for Custom Testing:

It’s a single line containing a name.

Sample Test Case 1:

 

Sample Input

STDIN      Function
-----      --------
Karthik → Name

Sample Output

Welcome Karthik.
It is our pleasure inviting you.

Have a wonderful day.Answer : 

#!/bin/python3


import math
import os
import random
import re
import sys



#
# Complete the 'Greet' function below.
#
# The function accepts STRING Name as parameter.
#

def Greet(Name):
    # Write your code here
    print(f'Welcome {Name}.')
    print ("It is our pleasure inviting you.")
    print ("Have a wonderful day.")
if __name__ == '__main__':
    Name = input()

    Greet(Name)

 

1. Namespaces 1

Python - Namespaces

Write the function definition for the function  'Assign' to assign the different types of variables in its parameters to new variables.

Parameters:

  1. An INTEGER in the variable 'i'
  2. A FLOAT in the variable 'f'
  3. A STRING in the variable 's'
  4. A BOOLEAN in the variable 'b'

New Variables to be assigned with :

  1. An INTEGER in the variable 'w'
  2. FLOAT in the variable 'x'
  3. STRING in the variable 'y'
  4. BOOLEAN in the variable 'z'

Assign the parameter variables Respectively. and

Print these in the following order:

  1. w
  2. x
  3. y
  4. z
  5. Display all the objects defined in the current namespace by using the 'dir' function

Input Format for Custom Testing:

# In the first line, value for 'i'

# In the second line, value for 'f'

# In the third line, value for 's'

# In the fourth line, value for 'b'

Sample Test Case 1:

Sample Input

STDIN      Function parameter
-----      ------------------
10  → i
3.14 → f
One → s
True → b

Sample Output

10
3.14
One
True
['b', 'f', 'i', 's', 'w', 'x', 'y', 'z']

No comments:

Post a Comment