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
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 → NameSample Output
Welcome Karthik.
It is our pleasure inviting you.Have a wonderful day.Answer :
#!/bin/python3
import mathimport osimport randomimport reimport sys## Complete the 'Greet' function below.## The function accepts STRING Name as parameter.#def Greet(Name):# Write your code hereprint(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:
- An INTEGER in the variable 'i'
- A FLOAT in the variable 'f'
- A STRING in the variable 's'
- A BOOLEAN in the variable 'b'
New Variables to be assigned with :
- An INTEGER in the variable 'w'
- FLOAT in the variable 'x'
- STRING in the variable 'y'
- BOOLEAN in the variable 'z'
Assign the parameter variables Respectively. and
Print these in the following order:
- w
- x
- y
- z
- 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