0% found this document useful (0 votes)
2 views32 pages

Python Multi-line Code and Formatting

The document covers Python programming concepts including multi-line code statements, escape sequences, and string formatting. It explains how to properly format strings, use escape characters, and perform numerical operations. Examples demonstrate the correct syntax and output for various coding scenarios.

Uploaded by

MEENAKSHI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views32 pages

Python Multi-line Code and Formatting

The document covers Python programming concepts including multi-line code statements, escape sequences, and string formatting. It explains how to properly format strings, use escape characters, and perform numerical operations. Examples demonstrate the correct syntax and output for various coding scenarios.

Uploaded by

MEENAKSHI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CSIT110

Python
Input & Output (2)

OBJECTIVES

● Multi-line code statement

● Escape sequence

● String format

● Numerical operations

2
Multi-line code statement

To end a statement in Python, you simply press Enter.


Therefore, this code will generate a syntax error:
subject_code = "CSCI111"
subject_mark = 80 Python thinks that this is
subject_grade = "D"
the end of the statement
result = "Subject result: "
+ subject_code
+ " mark " + str(subject_mark)
+ " grade " + subject_grade

print(result)

3
Multi-line code statement

Use the backslash \ to indicate that a statement


is continued on the next line.
subject_code = "CSCI111"
subject_mark = 80
subject_grade = "D"

result = "Subject
result: " \
+ subject_code \
+ " mark " +
str(subject_mark) \
+ " grade " +
subject_grade

print(result)
we can break a long line of code into multi-line
4
Multi-line code statement

Line continuation is automatic when the split comes while a


statement is inside parenthesis ( , brackets [ or braces {

Therefore, this code is fine:

subject_code = "CSCI111"
subject_mark = 80
subject_grade = "D"

print(
"Subject result: "
+ subject_code
+ " mark " +
str(subject_mark)
+ " grade " +
subject_grade
Sometimes,
) we should break a long line of code into multi-line
to make it clearer 5
Escape sequence

print("Welcome to Unimovies!")
print("Thursday July 30 at 7.15pm: Inside Out")

Program output:

Welcome to Unimovies!
Thursday July 30 at 7.15pm: Inside Out

6
Escape sequence

print("Welcome to Unimovies!")
print("Thursday July 30 at 7.15pm: Inside Out")

How do we write program for this output:

Welcome to Unimovies!
Thursday July 30 at 7.15pm: "Inside Out"

7
Escape sequence

How about this program?

print("Welcome to Unimovies!")
print("Thursday July 30 at 7.15pm: "Inside Out"")

what is wrong with this code?

We want to write a program for this output:

Welcome to Unimovies!
Thursday July 30 at 7.15pm: "Inside Out"

8
Escape sequence

The correct program

print("Welcome to Unimovies!")
print("Thursday July 30 at 7.15pm: \"Inside Out\"")

using escape sequence

Program output:

Welcome to Unimovies!
Thursday July 30 at 7.15pm: "Inside Out"

9
Escape sequence

Escape Sequence Meaning

\\ Backslash (\)

\' Single quote (')

\" Double quote (")

\n New line

\t Tab

10
Escape sequence

print("Your details:\n")
print("\tName: \"John Smith\"")
print("\tSN: \"2012345\"")
print("\nEnrolment record:\n")
print("\tMATH101") print("\
tCSCI201")

Program output:
Your details:

Name: "John Smith"


SN:

"2012345"

MATH101
Enrolment record:
CSCI201 11
Escape sequence

print("Escape sequence:")
print("\\n : Insert a newline.")
print("\\t : Insert a tab.")
print("\\\" : Insert a double quote character.")
print("\\\' : Insert a single quote character.")
print("\\\\ : Insert a backslash character.")

What is the output of this program?

12
String format

fname = "John"
lname = "Smith"
age = 20
gpa_score = 3.2

print("Hi {0}
{1}!".format(fn
ame, lname))
print("{1} {2} is {0} years old".format(age, fname, lname))
print("His GPA score is {0:.2f}".format(gpa_score))

print("Hi {0} {1}!".format(fname, lname))

print("{1} {2} is {0} years old".format(age, fname, lname))

print("His GPA score is {0:.2f}".format(gpa_score))


13
String format with alignment
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Lithiu
m", "Li", 3, 6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Sodium
Program output
", "Na", 11, 22.990))
print("{0:<15}{1:<10}
{2:^25} Alkali metals:
{3:>15}".format("Potass
ium", "K", 19, Element Symbol Atomic number Atomic weight
39.098)) Lithium Li 3 6.94
print("{0:<15}{1:<10}
Sodium Na 11 22.99
{2:^25} Potassium K 19 39.098
{3:>15}".format("Rubidi
Rubidium Rb 37 85.468
um", "Rb", 37, Caesium Cs 55 132.905
85.468)) Francium Fr 87 223
print("{0:<15}{1:<10}
{2:^25} 12345678901234567890123456789012345678901234567890123456789012345
{3:>15}".format("Caesiu 14
m", "Cs", 55,
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Lithiu
m", "Li", 3, 6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Sodium
", "Na", 11, 22.990))
left alignment, using 15 spaces
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Potass
ium", "K", 19,
39.098)) Alkali metals:
print("{0:<15}{1:<10}
{2:^25} Element Symbol Atomic number Atomic weight
Lithium
{3:>15}".format("Rubidi Li 3 6.94
um", "Rb", 37, Sodium Na 11 22.99
85.468)) Potassium K 19 39.098
Rubidium
print("{0:<15}{1:<10} Rb 37 85.468
{2:^25} Caesium Cs 55 132.905
{3:>15}".format("Caesiu
Francium Fr 87 223
m", "Cs", 55,
132.905)) 12345678901234567890123456789012345678901234567890123456789012345
print("{0:<15}{1:<10} 15
{2:^25}
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Lithiu
m", "Li", 3, 6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Sodium
", "Na", 11, 22.990))
left alignment, using 10 spaces
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Potass
ium", "K", 19,
39.098)) Alkali metals:
print("{0:<15}{1:<10}
{2:^25} Element Symbol Atomic number Atomic weight
Lithium
{3:>15}".format("Rubidi Li 3 6.94
um", "Rb", 37, Sodium Na 11 22.99
85.468)) Potassium K 19 39.098
Rubidium
print("{0:<15}{1:<10} Rb 37 85.468
{2:^25} Caesium Cs 55 132.905
{3:>15}".format("Caesiu
Francium Fr 87 223
m", "Cs", 55,
132.905)) 12345678901234567890123456789012345678901234567890123456789012345
print("{0:<15}{1:<10} 16
{2:^25}
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Lithiu
m", "Li", 3, 6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Sodium
", "Na", 11, 22.990))
center alignment, using 25 spaces
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Potass
ium", "K", 19,
39.098)) Alkali metals:
print("{0:<15}{1:<10}
{2:^25} Element Symbol Atomic number Atomic weight
Lithium
{3:>15}".format("Rubidi Li 3 6.94
um", "Rb", 37, Sodium Na 11 22.99
85.468)) Potassium K 19 39.098
Rubidium
print("{0:<15}{1:<10} Rb 37 85.468
{2:^25} Caesium Cs 55 132.905
{3:>15}".format("Caesiu
Francium Fr 87 223
m", "Cs", 55,
132.905)) 12345678901234567890123456789012345678901234567890123456789012345
print("{0:<15}{1:<10} 17
{2:^25}
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Lithiu
m", "Li", 3, 6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Sodium
", "Na", 11, 22.990))
right alignment, using 15 spaces
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Potass
ium", "K", 19,
39.098)) Alkali metals:
print("{0:<15}{1:<10}
{2:^25} Element Symbol Atomic number Atomic weight
Lithium
{3:>15}".format("Rubidi Li 3 6.94
< left
um", "Rb", 37, Sodium
85.468)) Potassium
Na
K
11
19
22.99
39.098
Rubidium
print("{0:<15}{1:<10} Rb 37 85.468
> right
{2:^25} Caesium Cs 55 132.905
{3:>15}".format("Caesiu
Francium Fr 87 223
m", "Cs", 55,
^ center
132.905)) 12345678901234567890123456789012345678901234567890123456789012345
print("{0:<15}{1:<10} 18
{2:^25}
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15.3f}".format("Lit
hium", "Li", 3, 6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15.3f}".format("Sod
ium", "Na", 11,
we would like to display the Atomic Weight
22.990))
print("{0:<15}{1:<10}
as having exactly 3 digits after the decimal mark
{2:^25}
{3:>15.3f}".format("Pot
Alkali metals:
assium", "K", 19,
39.098))
Element
print("{0:<15}{1:<10} Symbol Atomic number Atomic weight
{2:^25} Lithium Li 3 6.940
Sodium
{3:>15.3f}".format("Rub Na 11 22.990
Potassium
idium", "Rb", 37, K 19 39.098
85.468)) Rubidium Rb 37 85.468
Caesium
print("{0:<15}{1:<10} Cs 55 132.905
{2:^25} Francium Fr 87 223.000
{3:>15.3f}".format("Cae
sium", "Cs", 55,
12345678901234567890123456789012345678901234567890123456789012345
132.905)) 19
print("{0:<15}{1:<10}
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15.4f}".format("Lit
hium", "Li", 3, 6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15.4f}".format("Sod
ium", "Na", 11,
22.990))
print("{0:<15}{1:<10}
{2:^25}
{3:>15.4f}".format("Pot
Alkali metals:
assium", "K", 19,
39.098))
Element
print("{0:<15}{1:<10} Symbol Atomic number Atomic weight
{2:^25} Lithium Li 3 6.9400
Sodium
{3:>15.4f}".format("Rub Na 11 22.9900
Potassium
idium", "Rb", 37, K 19 39.0980
85.468)) Rubidium Rb 37 85.4680
Caesium
print("{0:<15}{1:<10} Cs 55 132.9050
{2:^25} Francium Fr 87 223.0000
{3:>15.4f}".format("Cae
sium", "Cs", 55,
12345678901234567890123456789012345678901234567890123456789012345
132.905)) 20
print("{0:<15}{1:<10}
print("Alkali metals:")
print()
print("{0:<15}{1:<10}
{2:^25}
{3:>15}".format("Element",
"Symbol", "Atomic number",
"Atomic weight"))
print("{0:<15}{1:<10}
{2:^25}
{3:>15.0f}".format("Lit
hium", "Li", 3,

6.94))
print("{0:<15}{1:<10}
{2:^25}
{3:>15.0f}".format("Sod
ium", "Na", 11,
22.990))
print("{0:<15}{1:<10}
{2:^25} Alkali metals:
{3:>15.0f}".format("Pot
Element
assium", "K", 19, Symbol Atomic number Atomic weight
39.098)) Lithium Li 3 7
Sodium
print("{0:<15}{1:<10} Na 11 23
{2:^25} Potassium K 19 39
Rubidium
{3:>15.0f}".format("Rub Rb 37 85
Caesium
idium", "Rb", 37, Cs 55 133
85.468)) Francium Fr 87 223
print("{0:<15}{1:<10}
{2:^25} 12345678901234567890123456789012345678901234567890123456789012345
{3:>15.0f}".format("Cae 21
sium", "Cs", 55,
String format with alignment

print("{0} x {1} = {2}".format(1, 5, 1*5))


print("{0} x {1} = {2}".format(2, 5, 2*5))
print("{0} x {1} = {2}".format(3, 5, 3*5))
print("{0} x {1} = {2}".format(4, 5, 4*5))
print("{0} x {1} = {2}".format(5, 5, 5*5))
print("{0} x {1} = {2}".format(6, 5, 6*5))
print("{0} x {1} = {2}".format(7, 5, 7*5))
print("{0} x {1} = {2}".format(8, 5, 8*5))
print("{0} x {1} = {2}".format(9, 5, 9*5))
print("{0} x {1} = {2}".format(10, 5, 10*5))

1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50 22
String format with alignment
print("{0:>2} x {1:>1} = {2:>2}".format(1, 5, 1*5))
print("{0:>2} x {1:>1} = {2:>2}".format(2, 5, 2*5))
print("{0:>2} x {1:>1} = {2:>2}".format(3, 5, 3*5))
print("{0:>2} x {1:>1} = {2:>2}".format(4, 5, 4*5))
print("{0:>2} x {1:>1} = {2:>2}".format(5, 5, 5*5))
print("{0:>2} x {1:>1} = {2:>2}".format(6, 5, 6*5))
print("{0:>2} x {1:>1} = {2:>2}".format(7, 5, 7*5))
print("{0:>2} x {1:>1} = {2:>2}".format(8, 5, 8*5))
print("{0:>2} x {1:>1} = {2:>2}".format(9, 5, 9*5))
print("{0:>2} x {1:>1} = {2:>2}".format(10, 5, 10*5))

1 x 5 = 5
2 x 5 = 10
we want a better output 3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50 23
Arithmetic operators

+ Addition 3 + 5 = 8
3 + 5.0 = 8.0
1.2 + 3.4 = 4.6
- Subtraction 5 - 2 = 3
5 - 2.0 = 3.0
6.5 - 1.2 = 5.3
* Multiplication 5 * 2 = 10
5 * 2.0 = 10.0
6.5 * 1.3 = 8.45

24
Arithmetic operators

/ Division 10/2 = 5.0


10/4 = 2.5
10/2.0 = 5.0
10.0/1.2 = 8.3333
// Floor division 10//2 = 5
10//4 = 2
10//2.0 = 5.0
10.0//1.2 = 8.0

What is the difference between Division and Floor division?

25
Arithmetic operators

/ Division 10/2 = 5.0


10/4 = 2.5
10/2.0 = 5.0
10.0/1.2 = 8.3333
// Floor division 10//2 = 5
10//4 = 2
10//2.0 = 5.0
10.0//1.2 = 8.0

Note that division of two integers give a decimal number


10/2 = 5.0

So if we want integer result, we should use Floor division


10//2 = 5 26
Arithmetic operators

** Exponent 10**2 = 100


10**4 = 10000
1.1**2 = 1.21

16**0.5 = 4.0
36**0.5 = 6.0

16**0.5 square root of 16

27
Arithmetic operators

% Modulus 15%2 = 1
124%10 = 4
28%2 = 0
37%5 = 2
-15%2 = 1

when x is an odd number: x%2 = 1


when x is an even number: x%2 = 0

to find the last digit of positive integers:


124%10 = 4
23%10 = 3

28
+= x += 2 is the same as x = x + 2
-= x -= 2 is the same as x = x - 2
*= x *= 2 is the same as x = x * 2
/= x /= 2 is the same as x = x / 2
//= x //= 2 is the same as x = x // 2
**= x **= 2 is the same as x = x ** 2
%= x %= 2 is the same as x = x % 2

29
Problem solving example 1

•Write a program that asks the user to enter name, age and GPA
score.
•Display this information exactly like the following example where
•- 1st column using left alignment and 15 spaces;
•- 2nd column using right alignment and 25 spaces.

•Enter first name: Frogory
•Enter last name: Green
•Enter age: 20
•Enter GPA score: 3.4
•Name: Frogory Green
•Age: 20
•GPA score: 3.4


Problem solving example 2

•A shop sells a product item for $10, but makes a discount that 3 items only cost $20. Write a
program to ask the user to enter the number of items they want to buy. Then the program displays
the cost.

•How much does it cost for 7 items? How much does it

cost for 12 items? How much does it cost for 14 items?

31
Thank you!

You might also like