Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Week 7, Python and excel, list, keyword, method, dictionary, Excel automation

The document provides an overview of Python data types, including single value types (integers, floats, strings, booleans) and collection types (lists, tuples, sets, dictionaries, ranges). It explains how to manipulate these data types, including indexing, modifying lists, and using methods like append, insert, and remove. Additionally, it covers Python keywords and introduces concepts like Excel automation and list comprehension for data analytics in public affairs.

Uploaded by

charlottelauyee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 7, Python and excel, list, keyword, method, dictionary, Excel automation

The document provides an overview of Python data types, including single value types (integers, floats, strings, booleans) and collection types (lists, tuples, sets, dictionaries, ranges). It explains how to manipulate these data types, including indexing, modifying lists, and using methods like append, insert, and remove. Additionally, it covers Python keywords and introduces concepts like Excel automation and list comprehension for data analytics in public affairs.

Uploaded by

charlottelauyee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Data Analytics and Visualisation for Public Affairs, PIA 3608

Week 7

Python and excel: list, keyword, method, dictionary, Excel automation

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:

• For single value:


• Integers
• Float
• String
• Boolean

• For a set of values:


• List
• Tuple
• Set
• Dictionary
• Range
Data Type- For single value
• Integers
• no limit to how long an integer value can be. Of course, it is constrained
by the amount of memory your system has, as are all things, but beyond
that an integer can be as long as you need it to be.

• 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:

• if else is based on whether the condition is True or False.


Data Type for a set of values
List
• Lists are used to store multiple items in a single variable.

• Lists are one of 4 built-in data types in Python used to


store collections of data, the other 3 are Tuple, Set, and
Dictionary, all with different qualities and usage.

• Lists are created using square brackets:

• list = [“Andy", “Alice", "cherry"]


Data Type for a set of values
Tuple
• Tuples are used to store multiple items in a single variable
• A tuple is a collection which is ordered and unchangeable.

• Tuples are written with round brackets (paren).

• tuple = ("apple", "banana", "cherry")


• Tuple items are ordered, unchangeable, and allow duplicate values.

• 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.

• A dictionary is a collection which is ordered, changeable and do not


allow duplicates.
• The values in dictionary items can be of any data type
• Index : value
Data Type for a set of values
Range
• The range() function is a built-in-function used in python, it is used to
generate a sequence of numbers.
• A range is a series of values between two numeric intervals.
• If you want to generate a sequence of numbers given the starting and
the ending values then you can give these values as parameters of the
range() function. For example:

• But you can also consider it as a data type. For example:

• The result is “range”, and it could be seen a data type.


Range
• The range() function defaults to 0 as a starting value,
• range (6) means range(0,6), means 0,1,2,3,4,5
• range (557) means range(0,557)

• it is also possible to specify the starting value by adding a


parameter: range(2, 6), which means values from 2 to 6 (but not
including 6)
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.
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 [ ]:

• Print the number of items in the list:

• 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 [ : ].

Not including item 6


Do more with the list
• By leaving out the start value, the range will start at the first item:

• 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:

• The value it returns is a Boolean type value (True or False).

• Since it’s a Boolean value, we can use this in if-else statement.


• “in” is a keyword
• Let’s know more about Python keyword
Python keywords
• Python keywords are special reserved words that have specific meanings
and purposes and can't be used for anything but those specific purposes.

• Every programming language has special reserved words, or keywords,


that have specific meanings and restrictions around how they should be
used. Python is no different. Python keywords are the fundamental
building blocks of any Python program.

• These keywords are always available—you’ll never have to import them


into your code.
Python keywords final!!! pls be familiar with the following
• Use help() function to knew all the keywords in Python
Python keywords
• Use help() function to knew all the keywords in Python

• 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:

• The value it returns is a Boolean type value (True or False).

• Since it’s a Boolean value, we can use this in if-else statement.


Do more with the list
• To change the value of a specific item, refer to the index number by [ ]:
• For example, change the second item:

• Change a range of item values:

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

• A Python method is similar to a Python function, but it must be called on an


object.
• Method is called by its name, but it is associated to an object (dependent).
• So firstly you should call the object name, and a “.”, and call the method name:
• Example 1:

• Example 2:
Do more with the list
• To add an item to the end of the list, use the append() method:

• To insert a list item at a specified index, use the insert() method:

The first parameter


should be the index

• 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.

• The remove() method removes the specified item.

• The pop() method removes the specified index.

• The pop() method removes the last item.


Do more with the list
The del keyword also removes the specified index:

• Delete the entire list by del:

• Delete the a range of items by del:

• You can also specify step as [start:stop:step]:


Python keywords
• Use help() function to knew all the keywords in Python

• 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:

• Delete the entire list by del:

• Delete the a range of items by del:

• You can also specify step as [start:stop:step]:


Do more with the list
• Loop Through a List. You can loop through the list items by using a for loop:

• Loop Through the Index Numbers


• You can loop through the list items by referring to their index number.

• Use the range() and len() functions to create a suitable iterable.


Do more with the list
• Create a list from range:

• Convert list to tuple/set:


Task
• Please delete the duplicate items of this list:

• 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~

• We will talk about more in workshop.


Sophisticated skills of Python
You are not a beginner any more
• You deserve to know more:

• Sophisticated skills “老司机”技巧:

• Covert data type between different iterables


• List comprehension
• For loop for more than one variables
• Play with string
Covert data type between
different iterables
Covert data type between different iterables
• List ordered, changeable, the content can be repeated
• Tuple ordered, unchangeable, the content can be repeated
• Set unordered changeable, the content can not be repeated

• Covert to list list()


• Covert to tuple tuple()
• Covert to set set()
Covert data type between different iterables
• To remove duplicate values in list:
• Take advantage of the characteristic of set
• Duplicate value is Not allowed in a set
• So?
Covert data type between different iterables
• To remove duplicate values in a list:
• Take advantage of the characteristic of the set:
• Duplicate value is Not allowed in a set
• Covert the list into a set, the set automatically removes duplicate values
• Covert the set back to list again→A new list with no duplicate values
Task
• Please remove duplicate values in the following list and write it to
the first line of a new excel file.
[1,2,3,1,2,3,1,2,3,4,5,6]
Task
• Please remove duplicate values in the following list and write it to
the first line of a new excel file as the colunms.
[1,2,3,1,2,3,1,2,3,4,5,6]

Try by your self first,


don’t read the answer until you have tried your best
Task:
• Please remove duplicate values in the following list and write it to the
first line of a new excel file. [1,2,3,1,2,3,1,2,3,4,5,6]
Excel automation: Batch vs Single

• Batch Process: Do all your similar tasks together at once.

• 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

• Is there an easier and more skilled way to do this?


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

• Is there an easier and more skilled way to do this?


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

• Is there an easier and more skilled way to do this?


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

• Is there an concise and sophisticated way to do this?


List comprehension
• a concise, sophisticated and elegant way to create lists.
• A list comprehension consists of:
• An expression and the for statement inside square brackets.

• a=[1,2,3,4,5]
• b=[i*i for i in a]

• An expression and the for statement inside square brackets.


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

• Is there an easier and more skilled way to do this?


List comprehension
• 3 line in one line

• vs

• beginner sophisticated player


Task
• Suppose you have the following list a. Please Create a new list in
which each term is three times the value of each term in list A.

• 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

• Is there an easier and more skilled way to do this?


For loop for more than one
variables
For loop for two variables
• Use function zip()
• Variable i from sequence a; Variable j from sequence b.
• If we want to iterate i and j together:

• for i , j in zip(sequence a, sequence b)


For loop for two variables
• Use zip()
• Variable i from sequence a; Variable j from sequence b.
• If we want to iterate i and j together:

• for i , j in zip(sequence a, sequence b)


加入去
• For example, suppose you have two lists, a and b.
• You want to multiply each pair of items in the same position in both
lists and put the results in a new list.
• a=[1,2,3,4,5]
• b=[5,4,3,2,1]
For loop for two variables
• Use zip()
• Variable i from sequence a; Variable j from sequence b.
• If we want to iterate i and j together:

• for i , j in zip(sequence a, sequence b)

• For example, suppose you have two lists, a and b.


• You want to multiply each pair of items in the same position in both
lists and put the results in a new list.
• a=[1,2,3,4,5]
• b=[5,4,3,2,1] 71
For loop for two variables
• Use zip()
• Variable i from sequence a; Variable j from sequence b.
• If we want to iterate i and j together:

• for i , j in zip(sequence a, sequence b)

• For example, suppose you have two lists, a and b.


• You want to multiply each pair of items in the same position in both
lists and put the results in a new list.
• a=[1,2,3,4,5] √
• b=[5,4,3,2,1] 72
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"]
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:

• My name is…and I am … years old. I am a student at …, majoring


in …. My hobby in spare time is….

• 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"

Please use for loop, and f-string to build your code.


Task
• Please print on the screen:
"This is the number 1"
"This is the number 3“
"This is the number 5"

... Until
"This is the number 21"

Please use for loop, and f-string to build your code.


Play with string
• Upper Case
• The upper() method returns the string in upper case:

• 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.

• Before attending the workshop, you can try using an AI assistant to


help you figure out what you're confused about. You are
encouraged to record your learning process with the AI assistant
and write a report to the lecturer.

• Remember this star button in anaconda.cloud? Its your AI assistant

90

You might also like