Sei sulla pagina 1di 22

PYTHON LANGUAGE

HUYNH VAN CHUNG


INTERNSHIP BATCH 2
Contents

 Python Introduction
 Python Regular expression
 File Handling
 Python Exercise

TMA Solutions 2
Python Introduction

 Developed by Guido van Rossum in the early 1990s.


 Python is a high-level, interpreted, interactive and object-oriented
scripting language.

 Easy integration with C, C++, COM, ActiveX, CORBA, and Java.

3
TMA Solutions 3
Python Introduction
 Basic syntax:

 Syntax to add libraries:
 Import + name:
 Some libraries often use: Time, Date time, Log...
 Variable and Syntax:
 Python has five standard data types: Number, String, List, Tuple,
Dictionary.
 Syntax:

4
TMA Solutions 4
Python Introduction
 String: String is one of the most common types in Python. String
in Python is immutable.

● Access values in string:

● Update string:

5
TMA Solutions 5
Python Introduction
 List: List in Python is a data structure that can store different data
types. In Python lists are written with square brackets.

 We can use list functions. For examples:

6
TMA Solutions 6
Python Introduction

 Dictionary: Dictionary in Python is an unordered set of key and


value pairs.
● Accessing value in Dict:

● Updating Dict:

7
TMA Solutions 7
Python Introduction
 The for loops: Repeats a set of statements over a group of
values.
● Syntax: for variableName in groupofValue:

statement(s)

Examples:

For loops can iterate over a sequence of numbers using the


"range" and "xrange" functions.

TMA Solutions 8
Python Introduction
 The while loops: While loops repeat as long as a certain boolean
condition is met.
● Syntax: while expression:

Statement(s)
● Examples:

TMA Solutions 9
Python Introduction
 if/else statements: Executes one block of statements if a certain condition is
True, and a second block of statements if it is False.

● Syntax:

● Examples:

TMA Solutions 10
Python Introduction
 Functions:
● A function is a block of code which only runs when it is called.

● You can pass data, known as parameters, into a function.

● A function can return data as a result.

● Examples:

11
TMA Solutions 11
Python Regular Expression
 A Regular Expression (RegEx) is a sequence of characters that
defines a search pattern.

 RegEx can be used to check if a string contains the specified


search pattern.
 Syntax :
 Funtion: allows to search a string for a match:
 Findall: return a list containing matches
 Exam:

12
TMA Solutions 12
Python Regular Expression
 Search: Returns a Match object if there is a match anywhere
in the string.
 Split: Returns a list where the string has been split at each
match.
Exam:

Result:

TMA Solutions 13
Python Regular Expression
 Sub: Replaces one or many matches with a string
Exam:

Result:

14
TMA Solutions 14
File Handling
 Python provides the basic functions and methods needed to
“File handling” as: open(), close(), read(), readline(), rename(),
remove(), …

 We use open (), usually, function in Python to open a file in read


or write mode.

 We have methods (modes) for opening a file:

TMA Solutions 15
File Handling

16
TMA Solutions 16
Python Exercise

 Exercise:Write a script to open an log file and print out Revision


(firmware), FSN (factory seri number), IMEI number, Model of
the module and the result of the test case (PASSED/FAILED)

17
TMA Solutions 17
Example
 Have a file : “L_QCT_SMSInbox_0001_App.log”
Write a script to open an log file and print out Revision(firmware)
FSN(factory seri number), IMEI number, Model of the module and
the result of the test case(PASSED/FAILED.

 Solution :
• First, use the command to open the file and process the
contents of the file: With…as
• Assign input value = f.readlines()
• Using For loop, variable “i”,check if “i” belongs to “input value “

18
TMA Solutions 18
Example

• Using Findall function in conjunction with Regular


Expression to search for “i”. Assign the newly found value
to a certain variable x
• Using Len() function to check the found value
• Print the found value.

19
TMA Solutions 19
Example
• Script :

20
TMA Solutions 20
Example

• Result:

21
TMA Solutions 21
THANK FOR LISTENING

8-10AM, Fri, July 4th 2019

THE END

22
TMA Solutions 22

Potrebbero piacerti anche