Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Python Introduction
Python Regular expression
File Handling
Python Exercise
TMA Solutions 2
Python Introduction
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.
● 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.
6
TMA Solutions 6
Python Introduction
● 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:
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.
● Examples:
11
TMA Solutions 11
Python Regular Expression
A Regular Expression (RegEx) is a sequence of characters that
defines a search pattern.
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(), …
TMA Solutions 15
File Handling
16
TMA Solutions 16
Python Exercise
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
19
TMA Solutions 19
Example
• Script :
20
TMA Solutions 20
Example
• Result:
21
TMA Solutions 21
THANK FOR LISTENING
THE END
22
TMA Solutions 22