Python Course - II Basic Programming
Python Course - II Basic Programming
Hand Notes 2
1
Python Programming - II
Convert Datatypes
int(), str(),float()
Sometimes we may want to construct strings from other information. This is where the format() method is useful.
# Learning Formatting Methods
discount = 10
offer = "newsletter"
print("Sign up for our {0} and get {1}% off.".format(offer, discount))
print("Enter your email here to get our {0}".format(offer))
Output
A string can use certain specifications and subsequently, the format method can be called to substitute those
specifications with corresponding arguments to the format method. The numbers in the format example are
optional, so you could have also written as:
# Learning Formatting Methods
discount = 10
offer = "newsletter"
print("Sign up for our {} and get {}% off.".format(offer, discount))
print("Enter your email here to get our {}".format(offer))
What Python does in the format method is that it substitutes each argument value into the place of the specification.
There can be more detailed specifications:
# Learning Formatting Methods with Numbers
print('{0:.3f}'.format(1.0/3))
# Fill with underscore(_) with text centered
# (^) to 11 width '____hello___'
print('{0:_^11}'.format('hello'))
#Left Align
print('{0:_<11}'.format('hello'))
#Right Align
print('{0:_>11}'.format('hello'))
2
Python Programming - I
Output
0.333
___hello___
hello______
______hello
Juan Manuel Santos wins Nobel Peace prize 2016
Abiy Ahmed Ali wins Nobel Peace prize 2019
ESCAPE SEQUENCES
You cannot specify 'What’s your name?' because Python will be confused as to where the string starts and ends. To
specify that this single quote does not indicate the end of the string, we use escape sequence. In Python \: backslash
character is used as escape sequence. Also, you have to indicate the backslash itself using the escape sequence \\ .
What if you wanted to specify a two-line string? One way is to use a triple-quoted string as shown previously or you
can use an escape sequence for the newline character - \n to indicate the start of a new line. Another useful escape
sequence to know is the tab: \t. There are many more escape sequences which we will discuss later. One thing to
note is that in a string, a single backslash at the end of the line indicates that the string is continued in the next line,
but no newline is added.
Examples
Output:
3
Python Programming - I
A physical line is what you see when you write the program. A logical line is what Python sees as a single statement.
Python implicitly assumes that each physical line corresponds to a logical line. If you want to specify more than one
logical line on a single physical line, then you have to explicitly specify this using a semicolon ( ; ) which indicates the
end of a logical line/statement. For example, all the 4 examples shown below have 2 logical lines:
However, I strongly recommend that you stick to writing a maximum of a single logical line on each single physical
line. The idea is that you should never use the semicolon.
INDENTATION
Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called
indentation. Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the
indentation level of the logical line, which in turn is used to determine the grouping of statements. This means that
statements which go together must have the same indentation. Each such set of statements is called a block. We will
see examples of how blocks are important in later chapters.
One thing you should remember is that wrong indentation can give rise to errors. For example:
Output:
#Type Conversion
var1 = "59" # implicit (by itself or automatically) conversion to String
print(type(var1))
var2 = float(var1) #explicit (outside/forced) conversion
print(type(var2))
print(var2)
### anything to integer: int()
### anything to float: float()
### anything to string: str()
4
Python Programming - I
sum = m1+m2+m3
print("Data type of sum is ",type(sum))
print("You have got total marks of ",sum)
sum=149
avg=sum/3
print("The total cost is {} and also total price is {} and the average cost is
{}".format(sum,sum,avg))
print("The total cost is {0} and also total price is {0} and the average cost is
{1}".format(sum,avg))
print("The total cost is {0} and also total price is {0} and the average cost is Rs.
{1:.2f}".format(sum,avg))
name="Sachin"
typ="Batsman"
pos="Opener"
print("{0} is a {1}".format(name,typ))
print("{0:_^14} is a {1:_<20} and comes at {2:_>15} position".format(name,typ,pos))
name="Saurav"
txt ="{}"
nl="\n"
print(" Hello, I am {} and I am going to talk about {} today!".format(name,txt))
print("Hello \nMy Name is Saurav \nHow are you today?")
print("\\n is used to print in nextline") # \ is Escape Sequence
print("\\\\n is used to print in nextline")
Assignments
1. # WAP to find average of marks in 5 subjects and print as below:
You have obtained marks are as 45,85,95,78 and 54 which gives total of 357 and average of 71.4
The input marks needs to be taken from the user input (Use INPUT())
- - - End of Session 3 - - -