0% found this document useful (0 votes)
12 views

Python - Lesson 2

hoc python 2

Uploaded by

Mit Jiu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python - Lesson 2

hoc python 2

Uploaded by

Mit Jiu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PYTHON

Tutor: Nguyễn Lê Chí Bảo


01 03

Introduction to Python Numpy


MODULE

02 04
Function Pandas
MODULE 2.
FUNCTION
Module 1:INTRODUCTION TO PYTHON

Introduction to Python

Basic syntax

Decision Making

Loops
Function
Module 2: FUNCTION

Function

A function is a piece of code in a program. The function


performs a specific task. The advantages of using functions are:
• Reducing duplication of code
• Decomposing complex problems into simpler pieces
• Improving clarity of the code
• Reuse of code
• Information hiding
Module 2: FUNCTION

Function
Here are simple rules to define a function in Python:
• Function blocks begin with the keyword def followed by the function name and parentheses ( ).
• Any input parameters or arguments should be placed within these parentheses. You can also
define parameters inside these parentheses.
• The code block within every function starts with a colon : and is indented.
• The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None.
Module 2: FUNCTION

Function
Define a function:

def function_name (parameters):


statement(s)
return values #optional

Example:
Module 2: FUNCTION

Function
Return multiple values: Function is defined use lambda:

Example:
File
Module 2: FUNCTION

File
Python provides inbuilt functions for creating, writing and reading files. There are two types of files that
can be handled in python, normal text files and binary files (written in binary language, 0s and 1s).
• Text files: In this type of file, Each line of text is terminated with a special character called EOL
(E Li ), which i h w i ch c (‘\ ’) i h .
• Binary files: In this type of file, there is no terminator for a line and the data is stored after
converting it into machine understandable binary language.
Module 2: FUNCTION

File
Module 2: FUNCTION

File
Opening a file

File_object = open(r"File_Name","Access_Mode")

Closing a file

File_object.close()
Module 2: FUNCTION

File
Writing to a file
• write() : Inserts the string str1 in a single line in the text file.
• writelines() : For a list of string elements, each string is inserted in the text file.
Used to insert multiple strings at a single time.

File_object.write(str1)
File_object.writelines(L) for L = [str1, str2, str3]
Module 2: FUNCTION

File
Reading from a file
• read() : Returns the read bytes in form of a string.
• readline() : Reads a line of the file and returns in form of a string.
• readlines() : Reads all the lines and return them as each line a string element in a list.

File_object.read()
File_object.readline()
File_object.readlines()
THANKS
CREDITS: This presentation template was created by
Slidesgo, including icons by Flaticon, and N : Ng ễ Lê Chí Bả
infographics & images by Freepik and illustrations Email: [email protected]
by Storyset Phone: 091 544 2420
Linkedin: nguyenlechibao

You might also like