0% found this document useful (0 votes)
5 views

Tôi đang chia sẻ 'Python4AI Intermediate' với bạn

The document is a Python textbook and workbook aimed at intermediate learners, authored by Dr. Nathan Ng. It covers various programming concepts including list access, user input, functions, and string manipulation, providing examples and exercises for practice. The text emphasizes the importance of programming skills in the context of digital transformation and personal development.

Uploaded by

aex47911
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)
5 views

Tôi đang chia sẻ 'Python4AI Intermediate' với bạn

The document is a Python textbook and workbook aimed at intermediate learners, authored by Dr. Nathan Ng. It covers various programming concepts including list access, user input, functions, and string manipulation, providing examples and exercises for practice. The text emphasizes the importance of programming skills in the context of digital transformation and personal development.

Uploaded by

aex47911
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/ 26

Python textbook and

workbook

Intermediate

(C) 2021 by Dr Nathan Ng

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

FOREWORDS 2Table of Content


4Level 11: Accessing a list
4Level 12: Accessing a range of a list
8Level 13: Getting user input
10Level 14: Functions
12Level 15: Functions and Input
15Level 16. Function with param
18Level 17. Function with 2 params
19Level 18. String, index, and len
21Level 19. String with if and else, len, index
22Level 20. Convert string to number
25

4
Level 11: Accessing a list
● We can access any item in a list by using a number

For example:

pets = [“cat”, “dog”, “chicken”]

To print the first item:

print(pets[0])

To print the second item:

print(pets[1])

To print the third item:

print(pets[2])

1. Define a list to store 2 best 2. Define a list to store 3


friend names. Print the first animals you like the most.
item Print the second item

names=["Dat","PA"] animals=["dog","pig","tiger"]
print(names[0]) for a in animals:
print(a)

3. Define a list to store names


of 2 teachers you love. Print
the first item.
teachers=["lan","hoa"]
print(teachers[0])
5
6. Create a list to store 3
foods that you like. Print
the second item.
foods=["hotdog","cream","sand"]
for f in foods:
print(f)

4. Define a list to store 2 7. Create a list to store 3


colors. Print the second cities that you visit. Print
item. the third item
colors=["red","blue"] citis=["Hanoi","TPHCM","DN"]
print(colors[1]) print(citis[2])

8. Create a list to store 3


countries that you want to
visit. Print the first item.
countries=["TB","HY",""HP]
for c in countries:
print(c)

5. Define a list to store 3


films that you like. Print 9. Create a list to keep 3
the third item. things in your house you use
the most. Print the first
films=["batman","ironman","ben10"] item.
print(films[2])
things=["bed","toilet","living"]
print(things[0])

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:

fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]

To print apple and mango we use:

print(fruits[0:2])

To print apple, mango, and kiwi, we use:

print(fruits[0:3])

To print mango and kiwi we use:

print(fruits[1:3])

1. Print from apple to banana

fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]


print(fruits[0:4])
8
2. Print from mango to banana
fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]
print(fruits[1:4])
7. Print from apple to mango
fruits = [“apple”, “mango”, “kiwi”, “banana”,“orange”]
print(fruits[0:1])
3. Print from kiwi to banana

fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]


print(fruits[2:4]) 8. Print from apple to kiwi

fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]


print(fruits[0:3])
4. Print from banana to orange

fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]


print(fruits[3:5]) 9. Print from kiwi to orange
fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]
5. Print from mango to orange print(fruits[2:5])
fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]
print(fruits[1:5])
10. Print all fruits

6. Print from apple to orange

fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]


print(fruits[0:5])
fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]
print(fruits[0:5])

fruits = [“apple”, “mango”, “kiwi”, “banana”, “orange”]


for i in fruits:
print(i)

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)

1. Ask How old is your daddy.


Print it out.
4. Ask How old is your cat?
Print it out.

2. Ask How old is your mommy.


Print it out.
5. Ask How old is your dog?.
Print it out.

3. Ask What is your school


name?. Print it out.
6. Ask What is your favorite
country?. Print it out.

10
9. Ask Who is your sister?.
Print it out.
7. Ask How old is your house.
Print it out.

10. Ask How old is your


sister? Print it out.
8. Ask How old is your car?
Print it out.

11
Level 14: Functions
Function is like a machine. It can help us do many things.

For example, we define a machine called hello() to print a message Hello


world

def say_hello():
print(“Hello”)

We need to run a machine by writing its name:

say_hello()

1. Define a function named 2. Define a function named


say_goodbye() to print say_goodbye() to print
message Good Bye message Good Bye

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

4. Define a function named


say_yes() to print message
Yes 8. Define a function named
sleep() to print message I am
sleeping

5. Define a function named


say_no() to print message No
9. Define a function named
wakeup() to print message I
wake up

6. Define a function named eat()


to print message I go to eat

10. Define a function named


learn() to print message I
learn programming

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)

1. Write a function to ask How


old is your daddy. Print it 2. Write a function to ask How
out. old is your mommy. Print it
out.

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.

8. Write a function to ask How


old is your car? Print it
4. Write a function to ask How out.
old is your cat? Print it
out.

9. Write a function to ask Who


is your sister?. Print it
5. Write a function to ask How out.
old is your dog?. Print it
out.

6. Write a function to ask What


is your favorite country?.
Print it out. 10. Write a function to ask
How old is your sister? 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

For example: function say_hello to say hello to a particular friend

def say_hello(name):
print(“Hello ”+name)

say_hello(“Ben”)

1. Define a function called


say_bye with a param called
name to print name of friend
you want to say bye

3. Define a function called sing


with a param called song to
print name of song you want
to sing

2. Define a function called eat


with a param called food to
print food you want to eat

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

Level 17. Function with 2 params


We can have a function with 2 inputs

For example:

def about_me(name, age):


print(“I am ”+name)
print(“My age is ”+age)

19
1. Define a function called name is ..”, “My sister age
my_sister with 2 params: is ...”
name, age. Print “My sister

4. Define a function called


my_sister with 2 params:
name, age. Print “My mom name
is ..”, “My mom age is ...”
2. Define a function called
my_brother with 2 params:
name, age. Print “My brother
name is ..”, “My brother age
is ...”

5. Define a function called


my_friend with 2 params:
name, age. Print “My friend
name is ..”, “My friend age
is ...”
3. Define a function called
my_dad with 2 params: name,
age. Print “My dad name is
..”, “My dad age is ...”

20
Level 18. String, index, and len
We know how to define a string variable:

name = ”Ben”

We use len() function to count how many letters a name has

We use name[0] to get the letter 0 of the string

name = “Ben”
print(len(name))
print(name[0])

1. Define a variable dad_name which contains your dad name.


Print first letter and length of the name

2. Define a variable mom_name which contains your mom name.


Print first letter and length of the name

21
3. Define a variable friend_name which contains your friend name.
Print first letter and length of the name

4. Define a variable friend_name which contains your friend name.


Print first letter and length of the name

5. Define a variable school_name which contains your school name.


Print first letter and length of the name

Level 19. String with if and else, len,


index
We can use if-else with string

For example:

name = “Ben”
print(len(name))
print(name[0])

Only print name if length is more than 2

22
if len(name)>2:
print(“Length is greater than 2”)

Only print name if the first letter is B

if name[0] == “B”:
print(“Name starts with B”)

6. Define a variable dad_name which contains your dad name.


Print a message if first letter is T

7. Define a variable mom_name which contains your mom name.


Print a message if length of the name > 3

8. Define a variable friend_name which contains your friend name.


Print a message if the first letter is B

23
9. Define a variable friend_name which contains your friend name.
Print name if the length is > 5

10. Define a variable school_name which contains your school name.


Print a message if the first letter is L

Level 20. Convert string to number


We use int() function to convert a string to a number

age = int(input(“Enter age”))


print(age)
if age>10:
print(“You are more than 10 years old”)

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

You might also like