Week 7, Python and excel, list, keyword, method, dictionary, Excel automation
Week 7, Python and excel, list, keyword, method, dictionary, Excel automation
Week 7
Dr. Li Ruozhu
City University of Hong Kong
ruozhuli@cityu.edu.hk
Data Type
• Variables can store data of different types, and different types can do
different things.
• Python has the following data types built-in by default, in these categories:
• 9 of them are the most common, and they're the ones we ask you to remember and
use in this course.
Data Type
• 9 most commonly used data types:
• Floating-Point Numbers
• float values are specified with a decimal point.
• String
• Strings are sequences of character data.
• String literals may be delimited using either single or double quotes. All
the characters between the opening delimiter and matching closing
delimiter are part of the string.
• Boolean Type
• Objects of Boolean type may have one of two values, True or False (Be
careful, the first letter should be capitalized.)
Boolean Type
• It is special, try to understand it.
• In programming you often need to know if an expression is True or False.
• You can evaluate any expression in Python, and get one of two answers, True
or False.
• When you compare two values, the expression is evaluated and Python
returns the Boolean answer:
• Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
Data Type for a set of values
Set
• Sets are used to store multiple items in a single variable.
• A set is a collection which is unordered, unchangeable, and
unindexed.
• Set items are unchangeable, but you can remove items and add
new items.
• Duplicate values are not allowed.
• Sets are written with curly brackets.
• set = {"apple", "banana", "cherry"}
Data Type for a set of values
Dictionary
• Dictionaries are used to store data values in key:value pairs.
• 9 of them are the most common, and they're the ones we ask you to remember and
use in this course.
More about list
Do more with the list
• List items are indexed and you can access them by referring to the
index number, by using [ ]:
Do more with the list
• List items are indexed and you can access them by referring to the
index number, by using [ ]:
Do more with the list
• List items are indexed and you can access them by referring to the
index number, by using [ ]:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Do more with the list
• List items are indexed and you can access them by referring to the
index number, by using [ ]:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
So, if you want to print the second item, you should enter:
Do more with the list
• List items are indexed and you can access them by referring to the
index number, by using [ ]:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
So, if you want to print the second item, you should enter:
Do more with the list
• List items are indexed and you can access them by referring to the
index number, by using [ ]:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
So, if you want to print the second item, you should enter:
Print(a[1])
Do more with the list
• List items are indexed and you can access them by referring to the index
number, by using [ ]:
• Negative Indexing:-1 refers to the last item, -2 refers to the second last item
• Range of Indexes:
• You can specify a range of indexes by specifying where to start and where to end the
range. When specifying a range, the return value will be a new list with the specified
items. Please using [ : ].
• By leaving out the end value, the range will go on to the end of the list:
• Specify negative indexes if you want to start the search from the end of the list:
Do more with the list
• Check if item exists:
• To determine if a specified item is present in a list use the in keyword:
• Some of them
you have already
used
Here we gonna
use in in list
Quick review of the keywords we've already used
• if elif else : to do the judgement
• while:to create a while loop
• for: to create a for loop
• def: to create a self defined function
• return: tell python what result you want
• or : indicate another situation
• import: to get module
• False and Ture: bool type data
• break: terminates the loop immediately when it's encountered. Use it
when you want to exit the loop entirely, e.g. to break out a for loop, or a
while loop.
Working of Python break Statement
• Now, you already know what is Python keyword
• Let’s go back to list
Do more with the list
• Check if item exists:
• To determine if a specified item is present in a list use the in keyword:
If you insert more items than you replace, the new items will be inserted where
you specified, and the remaining items will move accordingly:
Do more with the list
• Compare:
• Do the following two codes have the same result?
Do more with the list
• Compare:
• Do the following two codes have the same result?
Do more with the list
• Compare:
• Do the following two codes have the same result?
Replace the item specified in the original list with the items in square brackets
Use a new list as an item to replace the item specified in the original list
• To learn more about list,
• We need to know, what is “method” in Python
Function——Method
• Example 2:
Do more with the list
• To add an item to the end of the list, use the append() method:
• To append elements from another list to the current list, use the extend() method.
Do more with the list
• The clear() method empties the list. The list still remains, but it has no content.
• Some of them
you have already
used
Here we gonna
use del for the
first time
Do more with the list
The del keyword also removes the specified index:
• D=[1,2,3,3,3,3,3,4,5]
• Duplicate values are not allowed in set. So convert to set, you can
delete the duplicate items of a list.
Data Type: Dictionary
Dictionary
• A Python dictionary is a collection of items that allows us to store data in key: value
pairs.
• We have seen some of the basic logic of how iterable data types are used.
• Now, when we learn the dictionary, we can try to search some learning materials on
the Internet to try to learn by ourselves, such as:
• https://www.programiz.com/python-programming/dictionary
• It’s time to show your self-learning ability~
• Click is easy
• But hard to do batch processing
• So we need programming
Task
Suppose you have the following name list a. You want to create a
series of new empty excel files, and the name of each file is a
person's name in the list + Jan, for example, "Andy Jan".
janlist=["Andy","Alice","Ann","Amy"]
Task
Suppose you have the following name list a. You want to create a
series of new empty excel files, and the name of each file is a
person's name in the list + Jan, for example, "Andy Jan".
janlist=["Andy","Alice","Ann","Amy"]
List comprehension
Task
• Suppose you have the following list a. You want to create a new list b,
where you list the squared values of each of the items of list a.
• a=[1,2,3,4,5]
• How to do this?
• Remember?
• you have already know how to create a new list: b=[ ]
• how to add value to a list: b.append()
• how to use for loop: for … in …:
• And how to calculate squared value: i*i
• Try by your self first
• a=[1,2,3,4,5]
• How to do this?
• Remember?
• you have already know how to create a new list: b=[ ]
• how to add value to a list: b.append()
• how to use for loop: for … in …:
• And how to calculate squared value: i*i
• Try by your self first
• a=[1,2,3,4,5]
• How to do this?
• Remember?
• you have already know how to create a new list: b=[ ]
• how to add value to a list: b.append()
• how to use for loop: for … in …:
• And how to calculate squared value: i*i
• Try by your self first
• a=[1,2,3,4,5]
• How to do this?
• Remember?
• you have already know how to create a new list: b=[ ]
• how to add value to a list: b.append()
• how to use for loop: for … in …:
• And how to calculate squared value: i*i
• Try by your self first
• a=[1,2,3,4,5]
• b=[i*i for i in a]
• a=[1,2,3,4,5]
• How to do this?
• Remember?
• you have already know how to create a new list: b=[ ]
• how to add value to a list: b.append()
• how to use for loop: for … in …:
• And how to calculate squared value: i*i
• Try by your self first
• vs
• a=[1,2,3,4,5,9,10,333,666,777,999,3456789]
• How to do this?
• Remember?
• you have already know how to create a new list: b=[ ]
• how to add value to a list: b.append()
• how to use for loop: for … in …:
• And how to calculate squared value: i*i
• Try by your self first
a=["Andy","Alice","Ann","Amy"]
b=["1111","1112","1113","1114"]
Task
Suppose you have the following name list a, and ID list b. You want
to create a series of new empty excel files, and the name of each
file is an ID in list b+a person's name in the list a, for example,
"1111Andy".
a=["Andy","Alice","Ann","Amy"]
b=["1111","1112","1113","1114"]
Task
Suppose you have the following name list a, and ID list b. You want
to create a series of new empty excel files, and the name of each
file is an ID in list b+a person's name in the list a, for example,
"1111Andy".
a=["Andy","Alice","Ann","Amy"]
b=["1111","1112","1113","1114"]
For loop for more than one variables
• Zip also work for 3 variables
For loop for more than one variables
• Zip also work for 4 variables
Play with string
f-string
f string, a better way to insert variables into string
• In order to create and use strings efficiently, we found that we had to insert
variables into strings very often. It's a hassle to use “+”, commas, and str() all
the time.
• Let us make string interpolation simpler——f string
• Formatted string literals (also called f-strings for short) let you include the
value of Python expressions inside a string
• To create an f-string, prefix the string with the letter “ f ”. In the string, just
wrap the variable in { } wherever it needs to be inserted.
• F-strings provide a concise and convenient way to embed python expressions
inside string literals for formatting.
• Directly calculate
Task
• Please ask the user to enter their name, age, major, school name, and
hobbies. Then please display all the results in full English sentences on
the screen:
• For example:
• My name is Andy and I am 19 years old. I am a student at City
University, majoring in public policy. My hobby in spare time is piano.
• Backslashes \
• are used to wrap lines and have no effect on the code running.
Task
• Please print on the screen:
"This is the number 1"
"This is the number 2"
... Until
"This is the number 20"
... Until
"This is the number 21"
• Lower Case
• The lower() method returns the string in lower case:
Play with string
• Replace String
• The replace() method replaces a string with another string:
Play with string
• Split String
• The split() method returns a list where the text between the specified
separator becomes the list items.
• splits the string into substrings if it finds the separator:
• If the separator is “,”
• Let’s try:
See you in the face to face workshop
• In the following workshop, the lecturer will also take you hand in
hand to practice all the tasks above again, step by step.
90