Python Basics- Lesson-5
Good Morning Boys.
Welcome to the fifth session of the online classes for Python Programing.
Let us look at the solutions of the Exercise given yesterday:
1. Accept a number from the user . Display an appropriate message .
num=input("Enter a number")
2. Accept a name from the user . Display an appropriate message .
name=input("Enter your name")
3. Accept the name and age of the user. Display appropriate messages.
name=input("Enter your name")
age=input("Enter your age")
4. Accept two numbers from the user. Display appropriate messages before taking the input.
num1=input("Enter the first number")
num2=input("Enter the second number")
5. Accept a number from the user and typecast it to float.
num=float(input("Enter the number"))
If you have any doubt,feel free to share.
_________________________________________________________________
By the end of this session, you will be able to
- Understand the purpose and syntax of print( ) function
- Impact of various formats of Print( ) functions.
The new topic of discussion today is the print () function.
PURPOSE:
The
print() function prints the specified message to the screen, or other standard output device.
The message can be a string, or any other object, the object will be converted into a string before written to the screen.
5.1. Quick Example
In order to print the Hello world string, use the print() function as follows:
is the correct way to print a string. Note; however, that is Python 2.X, you could also write:
5.2. print blank line(s)
Let us print 5 blank lines. You can naively type:
or:
or even better:
5.3. Print an object without a trailing newline
By default, the print function add a trailing line. To prevent this behavious, add a comma after the statement:
you could use:
5.4 Special characters
To escape the sign %, just double it:
Other special characters similar to some other languages are summarized in the following table:
| character | Decimal | Description |
|---|---|---|
| \ | statement continues on next line | |
| \ | 92 | backslash |
| \’ | 39 | Single quote |
| \” | 34 | Double quote |
| \a | 7 | Bell |
| \b | 8 | Backspace |
| \f | Formfeed | |
| \n | 10 | newline |
| \r | 13 | carriage return |
| \t | 9 | tabulation |
| \v | 11 | vertical tabulation |
| \0 \000 | null value | |
| \ooo | octal value o in (0..7) | |
| \xhh | hexadecimal value (0..9, a..f; A..F) | |
| \uxxxx | Unicode character value |
EXERCISE
Write the output of the following program:
Q1)
name = Input("Enter your name : ") # suppose the user enters John
L
= float(input "Enter length of Rectangle : ")) # suppose the user enters 12
B
= float(input("Enter breadth of Rectangle : “) # suppose the user enters 24
Area
= L * b
print("Hello!", name)
print("The
area of Rectangle is : " , Area)
B = 3
C = 200
D = C + B - A
C = (D - (D % 10)) * 10
print(D//10, " KM equals
to ", C*10," Meters ")
print("Dist. from Delhi
to Ludhiana is",4**B + A, "KM")
NOTE %- is known as the MODULUS operator and is used to get remainder on division. //- is known as the integer division operator and is used to get quotient in integer form
**- is known as the EXPONENTIAL operator and is used to raised to power.
**- is known as the EXPONENTIAL operator and is used to raised to power.
Good morning ma'am
ReplyDelete~Emmanuel
Good morning Dear. there is an update. Pls refresh your page
DeleteGood Morning ma'am
ReplyDelete-Ashish
Good Morning to all.
Deletegood morning maam
ReplyDelete_clement
Good morning ma'am
ReplyDelete-Geet
I expect all of you to
ReplyDelete1. check answers to yesterday's HW first
2. Understand the print function- various syntax (Ways of writing)
3. Perform the last 2 questions and share the answers.
I am waiting for any clarification and then the answers
Good morning mam
ReplyDeletegood morning ma'am
ReplyDelete-Artham
GOOD MORNING MAM
ReplyDeleteAARON D
Ma'am i'm not able to understand the concept of PRINT AN OBJECT WITHOUT A TRAILING NEWLINE.
ReplyDelete-Geet
Beta trailing means at the end. When u dont want newline at the end (Which is default), we use this method
DeleteThe print function add a trailing line by default. To prevent this behavious, add a comma after the statement.
>>> x, y = 1, 2
>>> print(x),; print(y)
1 2
As u can see, 2 is on the same line as 1.
Focus on this line, note the use of comma
Deleteprint(x),; print(y)
I expect doubts to be put up on the group pls so that all can benefit
ReplyDeleteThanks a lot ma'am
ReplyDelete-Geet
Have you understood Geet?
DeleteYes ma'am
ReplyDelete-Geet
This comment has been removed by the author.
ReplyDeleteMa'am i've completed the work
ReplyDelete-Geet
Ma'am the answer for 1) would come as a syntax error bcoz we never write input in uppercase and in the AREA formula length is written in uppercase whereas earlier it was written in lowercase.
ReplyDelete-Geet
Geet you have forgotten, the keywords are ALWAYS written in small/lower case
Deleteanswer no 2 , (12,'km equals to ',1200,'meters)
ReplyDelete('dist. from Delhi to Ludhiana is ',139,'km )
what about the other question???
DeleteOthers ????
ReplyDeleteAshish your answer is correct.
ReplyDeleteIts said that the others are not making any effort.
Pls solve both the questions.
All notes to be copied in your registers.
See u in the evening zoom session. Keep your computer ready with Python. We will be doing practicals
Its sad**
Delete