004 - 14-05-2020 XI-Python Basics - Programming #2
004 - 14-05-2020 XI-Python Basics - Programming #2
Computer Science
Programming with Python - 2
Topics to be covered 2
Python Logical Operators are used to operate upon Boolean Values and
return a single result as either True or False:
Multi-line statements
In Python, end of a statement is marked by a newline character (by
pressing the enter key). However, a statement can be extended over
multiple lines with the line continuation character (\). For example:
x = 2 + 4 + \ These 3 lines are equivalent to the
6 + 8 + \ single line statement
10 + 12 x = 2 + 4 + 6 + 8 + 10 + 12
Python Statements 10
Python Hello
print("Hello\nFriends") code
Friends
print('We aren\'t afraid')
We aren't afraid
print("Just \t tabbed")
Just tabbed
print("My Car __/===\\__") Output My Car __/===\__
print() - Displaying outputs 13
The print() function prints the specified message to the screen. The
message can be a declared and initialized variable, any literal, or the
result of any operation. Example:
Python
print('Hello World') Hello World
code
print("Double quotes") Double quotes
print('concatena'+'tion') concatenation
print('hello','there') hello there
print('I\'m 5') I'm 5
Output I'm 5
print("I'm 5")
He said "hi"
print('He said "hi"')
print() - Displaying outputs 14
Parameter Description
object(s) Any object (variable/literal/calculation). If there
are multiple objects, they may be separated by
comma.
sep=<separator> Optional. Used to specify what character to be
used to separate multiple objects to be printed
with a single print(). Default is space ' '
end=<end> Optional. Used to specify what character
should be printed at the end. Default is '\n'
(line feed)
print() - Examples 16
print ('Hello')
Hello
print ('World')
World
Taking user input 17
Programmers often need to interact with users to get data for processing.
Python provides us with the inbuilt function input() to take a user input for
any data. The value entered by the user is returned back by the input() as
a string.
Syntax:
<Var>=input( <prompt> )
Parameter Description
prompt A String, representing a default message before the
input.
Taking user input 18
● When input() function executes the program flow stops until the user
has given an input.
● The prompt is displayed on the output screen to ask a user to enter
input value (However, the prompt, to be printed on the screen is
optional with input()).
● The user input value is accepted as a string and returned back by the
input(). Even a number entered with input() is treated as a string.
Thus to input a number as an integer or a float value, an explicit
conversion is needed from string to an int or float respectively.
Taking user input 19
Principal:7000
for user Inputs as
Rate:10
7000,10 and 5
Output → Time:5
Simple Interest: 3500.0
While conversion, keep the following in 23
mind
P = float(input('Principal:')) While converting the input data
to int or float, remember - if we
R = float(input('Rate:')) enter string, it will raise an
T = float(input('Time:')) exception. The program will not
SI = P*R*T/100
execute further.
print("Simple Interest:",SI)
Principal:7000
Rate:10
Time:Good Time
----------------------------------------------
ValueError: could not convert string to float: 'Good Time'
Simplicity of Instructions 24
You do not write programs for executing it once by the computer, you
need to write clear instructions so that any whosoever reads the
program later (even you yourself!!) should be able to understand.
A=float(input("A:")) P=float(input("P:"))
B=float(input("B:")) R=float(input("R:"))
C=float(input("C:")) T=float(input("T:"))
D=A*B*C/100 SI=P*R*T/100
print(D) print(SI)
Simplicity of Instructions 25
S=float(input("Sal:"))
IH=S+S*(30/100)+S*(10/100)
print(IH) Sal=float(input("Sal:"))
DA=Sal*(30/100)
Tax=Sal*(10/100)
InHand=Sal+DA-Tax
print(InHand)
Clarity & Simplification of expression 26
● Syntax Error: Syntax error, also known as parsing error, is the most
common kind of error in the program, which occurs due to wrong use
of syntax of the language. The process of translation of the script to
be understood by the system is known as parsing. The parser stops
translation at the offending line (line with wrong syntax) and displays
a little ‘arrow’ pointing at the earliest point in the line where the error
was detected.
Syntax Errors - Examples 28
Thank You
Computer Science Department
Delhi Public School, R.K.Puram