APPLICATION LABORATORY II
Sherina Sally
Python Fundamentals Department of ICT
Faculty of Technology,
University of Colombo
OUTLINE
• Python Indentation
• Python Statements and Comments
All Rights Reserved
• Python Keywords
• Basic I/O functions
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 2
1
INDENTATION IN PYTHON
• C++, C, Java, C# uses indentation for readability
• Python uses to indicate a block of code
All Rights Reserved
• At least one space is required
• Use the same amount of spaces for all the lines in the block
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 3
STATEMENTS
• Instructions which the Python interpreter can execute
• Simple statements denote a single line of code
• No semicolon (;) at the end
All Rights Reserved
• Multiple simple statements can occur in a single line: separate using (;)
• Multi-line statements denote a continuation of a statement
• Use the continuation character (\) or parentheses ( ), [ ], { }
• Compound statements contains a group of other statements
• Spans across multiple lines
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 4
2
STATEMENTS …(2)
if x > 1:
x = 10 y = “Hello ” \ print(“X is greater than 1")
“I’m Ann. ”\ else:
Simple statement “What is your name ? ” print(“X is less than 1")
All Rights Reserved
Compound statement
x = 10; p = 20; q = 4
z = (1 + 2 + 3 +
Simple statements in a single line 4+5+6+
7+8+9)
Multi-line statement
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 5
COMMENTS
• Increase understandability of a program
# A single line comment
• Ignored by the interpreter
• Single line comments can be applied by using (#) # A multi-line comment
All Rights Reserved
# can be denoted
# as a single line comment
• No explicit way of denoting a multi-line comment
# in multiple lines
• Use multiple single line comments
• Use triple single quotes (‘ ‘ ‘ ) or triple double quotes (“ ” ”) ‘’’ This is another way
to denote a
multi-line comment ‘’’
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 6
3
RESERVED KEYWORDS
False await except in raise
None break finally is return
True class for lambda try
All Rights Reserved
and def from nonlocal while
as del global not with
assert elif if or yield
async else import pass
Python version 3.9.0
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 7
USER INPUTS
• User inputs are important when the program needs values externally
• input() function gets a text value as user input
All Rights Reserved
x = input(“Enter your name:”)
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 8
4
DISPLAY OUTPUT
• Outputs that are intended to display by the program to the console
• print() function prints the text between the quotes
print(‘ Welcome to Python ‘)
All Rights Reserved
• Displays dynamic values
a = “Micheal”
print(“ First program of “, a)
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 9
FORMATTED OUTPUT …(1)
• Similar to placeholders in C language
% [flags] [width] [.precision] type
Type of conversion
All Rights Reserved
Applies to certain conversions
Digits after the decimal point
Minimum width of the output
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 10
5
FORMATTED OUTPUT …(2)
<type> Conversion Type
print(“%4.2f“ %(3.235)) d, i, u Decimal integer >>> print(“ %3d “ %1)
x, X Hexadecimal integer
o Octal integer
All Rights Reserved
f, F Floating point
e, E Exponential >>> 1
3.23
g, G Floating point or Exponential
c Single character
s, r, a String
% Single '%' character
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 11
Department of ICT, Faculty of Technology, University of Colombo