You are on page 1of 20

Introduction to Python

Tharaka Ilayperuma
Content

PART I – input and output in Python

PART II – Control Structures (Introduction)


Input and Output
Output functions

You can either;


display data in a human readable form using the print() function, OR
write to a file for future use
print() function

The print() function is used to display output

The print function can take a number of arguments - each has a default value

Object (variable) that


you are going to
output
print(name, sep=' ', end='\n', file=sys.stdout, flush=False)

print(“Hello”) This will result in applying the default values for the arguments above
• New lin
print() function – examples (1)
The end keyword argument

Printed on two lines

Printed on one line

The sep keyword argument


Positional arguments
concatenated into a single
string
Use of separator to
concatenate the positional
arguments
print() function – examples (2)
Printing to a file
By default, print() is bound to sys.stdout through its file argument

Use file keyword argument to indicate a file that was open in write or append mode to
write messages to it
This statement is creating/opening the file called “output.txt”
and creates alias “f” to access the output.txt file
Formatting your output (1)
Different formatting options are available to display the results as per the user
requirements
• formatted string literals (f-string) https://docs.python.org/3/tutorial/inputoutput.html
• str.format() method
Read more on
formatting options
Using formatted string literal or f-string to format your output
here
e.g. year = 2023
course = ‘Introduction to Python’
print(f'Results of the course: {course} in {year}’)

Output:
Results of the course: Introduction to Python in 2023

let you include the value of Python expressions inside a string by prefixing
the string with f or F and writing expressions as {expression}
Formatting your output (2)
Using str.format() to format your output • When placeholders { } are empty, Python will
replace the values passed through
str.format() in order
• each individual value contained in the format
method can be called by its index number,
which starts with the index number 0
Formatting your output (3)
Type setting and padding

• 9 specifies the minimum width/padding the number (12530.35) can take


• 2 truncates the decimal part (.35) up to the given decimal points – in this case 2
Formatting with alignment

Type Meaning

Left aligned to the


<
remaining space

Center aligned to the


^
remaining space

Right aligned to the


>
remaining space

Forces the signed (+) (-) to


=
the leftmost position
Input() function
To get user input via keyboard, you can use input() function

Note: Python takes all the input as a string input by default

If you need to convert the user input to any other type, you need to do it explicitly
• num = int(input("Enter a number: "))

# Input output demonstration

name = input ("Enter your name: ") >>> Enter your name: Philip Windridge

email = "p.c.windridge@staffs.ac.uk"

print("Name:", name, "Email:", email)

>>> Name: Philip Windridge Email: p.c.windridge@staffs.ac.uk


Multiple inputs
You can use split() method to take multiple inputs at once

split() method breaks the given input by the specified separator. If a separator is not
provided then any white space is a separator

a, b, c = int(input("Enter the Numbers : ")).split()


print("The Numbers are : ",end = " ")
print(a, b, c)
Exercise

Write a Python program to read the First name, Last name, and salary of an employee of
an employee and display the output in the following manner.

Phil James ==> 50000


Control structures (Sequence, selection & iteration)
Control Structures (Sequence, Selection &
Iteration)
What is a control structure?
Control structure determines the execution flow of your code

Sequence Selection Iteration


Start Start Start

false true false


S1 C C

S2 S1 S2 true

S1
S3
End
End End

e.g. if statement, e.g. for statement,


match statement while statement
Summary

We looked at input, output, output formatting, and


the different types of control structures in Python
Questions?

You might also like