Python Fundamentals II
Python Fundamentals II
will print output as This time the print(0ended the line with
given end character. which is'S here
old.
My name is Amit. $I am 16 years
NOTE
So theend argumentdetermines the
end character that will be
printed at the endof print line. In print() function, the default
value of end
Code fragment 1
newline argument is
character (\n') and of
a, b= 20, 30 sep argument, it is space ()
Notice first print
statement has end
print ("a =", a, end = )
set as a space
print ("b =", b)
Check
Point
The reason for above output is quite clear. Since there is
end character given as a space (1.e., end = ) in first print
74 statement, the newline (\n') character is not appended at
1. What is a variable ?
the end of output generated by first print statement.
Thus the output-position-cursor stays on the same line.
2. Why is a variable called symbolic
variable ? Hence the output of second print statement appears in the
3. Create variables for the following
same line. Now can you predict the output of following
() to hold a train number code fragment ?
(ii) to hold the name of a subject Name ='Enthusiast'
(iii) to hold balance amount in a bank print ("Hello", end = )
accournt
print (Name)
(iv) to hold a phone number print ("How do you find Python ")
4. What do you mean by dynamic typing
of avariable ? What is the caution you Well, youguessed it right. O It is:
must take care of ?
Hello Enthusiast
5. What happens when you try to access
the value of an undefined variable ? How do youfind Python ?
6. What is wrOng with the following
statement ?
In Python you can break any statement by putting a \ i5
the end and pressing Enter key, then completing he
Number = input ("Number") statement in next line. For example, following statement is
Sqr = Number*Number
perfectly right.
7. Write Python code to obtain the
balance amount. print ("Hello", \ The backslaslh at theend means
that the statement is still
8. Write code to obtain fee amount and end= ) continuing in next line
then calculate fee hike as 10% of fees
(i.e., fees x 0.10). Nowconsider following sample programs.
Chopter7: PYTHON FUNDAMENTALS
187
7.1 Write a program to input a welcome message and print it.
7.3 Program to obtain length and breadth of a rectangle and calculate its area.
am
grar # to input length and breadth of a rectangle and calculate its area
length = float( input("Enter length of the rectangle: "))
breadth = float( input ("Enter breadth of the rectangle:"))
area = length * breadth
here.
Theoutput produced by above program is as shown
miloc)
7.6 Write a program to input a value in kilometres and convert it into miles (1 km = 0.621371
7.7 Write a program to input a value in tonnes and convert into quintals and kilograms.
(1 tonne 10 quintals 1tonne =1000kgs, 1quintal = 100kgs)
rogram
189
7.9 Write a program to input two
numbers and swap them.
n1 = int( input ("Enter
rogram number 1 :"))
n2 = int( input ("Enter
number :"))2 Sample Run:
print ("Original numbers :", n1, n2)
n1, n2 = n2, n1 Enter number 1: 17
Enter number 2: 28
print("After swapping :", n1, n2) Original numbers : 17 28
After Swapping : 28 17
r 10 Writea program to input three numbers and
swap them as this : 1st
nd number becomes the 3rd
number and the 3rd number becomesnumber becomes the 2nd number,
the first number.
rogram
X= int (input("Enter number 1:"))
y =int( input("Enter number 2 :") ) Sample Run :
z = int( input("Enter number 3 :")) Enter number 1:6
print("Original numbers:", x, y, z) Enter number 2: 11
X, y, z = y, Z, x Enter number 3: 9
LET US REVISE
APython program cancontain various components like expressions, statements, comments, functions, blocks and indentation.
* Anexpression is a legal combination of symbols that representS a value.
Astatement is aprograming instruction.
* Comments are non-executable, additional information added in program for readability.
* In Python, comments begin with a # character.
* Comments can be single-line comment. multi-line comments and inline commnents.
* Function is anamed code that can be reused with a program.
A block/suitelcode-block is aaroun of statements that are part of another statemen.
*Blocks are represented through indentation.
Avariable in Python is defined only when some value is assigned to t.
hold values of different types at different times.
. osupporIS dynamic tvping ie.. a varigble can
always returns a string type of value.
pu s used to obtain input from user : it
Ouput is generated throuah printí )(by callingprint function) statenen.