Tôi đang chia sẻ 'Python4AI Intermediate' với bạn
Tôi đang chia sẻ 'Python4AI Intermediate' với bạn
workbook
Intermediate
1
“Everybody should learn to program a computer, because it teaches you how
to think.”- Steve Jobs
FOREWORDS
Computer, Telecom, Artificial Intelligence is changing our lives. Some get
better living, some go unemployed. The wave of change that entails every
organization and person to transform digitally. Now or Never, Change or Die.
That imperative trend insists every of us to be part of the global reshaping or
to be completely excluded. Personal Digital Transformation opens a call for
everyone to participate. For a better life for us now and our next generations.
2
“
1. “Any fool can write code that a computer can understand. Good programmers write code
that humans can understand.” – Martin Fowler
2. “First, solve the problem. Then, write the code.” – John Johnson
3. “Experience is the name everyone gives to their mistakes.” – Oscar Wilde
4. “ In order to be irreplaceable, one must always be different” – Coco Chanel
5. “Java is to JavaScript what car is to Carpet.” – Chris Heilmann
6. “Knowledge is power.” – Francis Bacon
7. “Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week
debugging Monday’s code.” – Dan Salomon
8. “Perfection is achieved not when there is nothing more to add, but rather when there is
nothing more to take away.” – Antoine de Saint-Exupery
9. “Ruby is rubbish! PHP is phpantastic!” – Nikita Popov
10. “ Code is like humor. When you have to explain it, it’s bad.” – Cory House
11. “Fix the cause, not the symptom.” – Steve Maguire
12. “Optimism is an occupational hazard of programming: feedback is the treatment.
“ Kent Beck
13. “When to use iterative development? You should use iterative development only
on projects that you want to succeed.” – Martin Fowler
14. “Simplicity is the soul of efficiency.” – Austin Freeman
15. “Before software can be reusable it first has to be usable.” – Ralph Johnson
16. “Make it work, make it right, make it fast.” – Kent Beck
”
3
Table of Content
4
Level 11: Accessing a list
● We can access any item in a list by using a number
For example:
print(pets[0])
print(pets[1])
print(pets[2])
names=["Dat","PA"] animals=["dog","pig","tiger"]
print(names[0]) for a in animals:
print(a)
6
10. Create a list to save 4
places in Ho Chi Minh city
that you like. Print the
second item.
places=["BR","SG","VT","BT"]
print(place[1])
7
Level 12: Accessing a range of a list
We can access not only 1 item but a list of items by using 2 numbers.
For example:
print(fruits[0:2])
print(fruits[0:3])
print(fruits[1:3])
9
Level 13: Getting user input
● We use input() to ask a question and get the answer from user
For example:
name = input(“What is your name?”)
print(name)
For example:
age = input(“How old are you?”)
print(age)
10
9. Ask Who is your sister?.
Print it out.
7. Ask How old is your house.
Print it out.
11
Level 14: Functions
Function is like a machine. It can help us do many things.
def say_hello():
print(“Hello”)
say_hello()
12
3. Define a function named
say_love_you() to print
message I love you 7. Define a function named
drink() to print message I
want to drink
13
14
Level 15: Functions and Input
● We use input() to ask a question and get the answer from user
● We use def to define a function
● Let’s combine them
For example:
def get_name():
name = input(“What is your name?”)
print(name)
For example:
def get_age():
age = input(“How old are you?”)
print(age)
15
3. Ask What is your school
name?. Print it out.
7. Write a function to ask How
old is your house. Print it
out.
16
17
Level 16. Function with param
A function is like a machine, remember?
We can make our machine more useful giving it some input
def say_hello(name):
print(“Hello ”+name)
say_hello(“Ben”)
18
5. Define a function called
study with a param called
4. Define a function called subject to print I study +
visit with a param called subject
friend to print I am visit +
name of friend
For example:
19
1. Define a function called name is ..”, “My sister age
my_sister with 2 params: is ...”
name, age. Print “My sister
20
Level 18. String, index, and len
We know how to define a string variable:
name = ”Ben”
name = “Ben”
print(len(name))
print(name[0])
21
3. Define a variable friend_name which contains your friend name.
Print first letter and length of the name
For example:
name = “Ben”
print(len(name))
print(name[0])
22
if len(name)>2:
print(“Length is greater than 2”)
if name[0] == “B”:
print(“Name starts with B”)
23
9. Define a variable friend_name which contains your friend name.
Print name if the length is > 5
24
1. Ask user how many apples they have and convert it to number
2. Ask user how many oranges they have and convert it to number
3. Ask user how many pens they have and convert it to number
4. Ask user how many pets they have, convert it to a number, and check if
it is > 2 then print a message: You have more than 2 pets
25
5. Ask user how many books they have, convert it to a number, and check if
it is > 3 then print a message: You have more than 3 books
26