Week 2, Python Comments, input, Data type convert, if else
Week 2, Python Comments, input, Data type convert, if else
Dr. Li Ruozhu
City University of Hong Kong
[email protected]
• Let’s review comment, data type, and function, and
learn a little bit more new things about them~
Enter comment: Why we need
comments?
• 1 Write to yourself. Because later maybe you will forget why you wrote this line. Once
forgotten, it is difficult to continue the project.
• 2 Write to your teammates. Facilitate teammates to immediately understand your purpose,
efficient teamwork.
• 3 Show the students when explaining. It is easy for students to understand what is going
on at the moment.
• 4 When writing programs, temporarily save the content that you don't need but don't want
to delete, so that the process of thinking and input could be smoother.
• Format:
• Starting by #
• Enter human language naturally
3
3 Ways to Create Comments
• For single line:
• #
• 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, please try the below lines, and check the answers in terminal :
• 2 String
“apple”
• 3 Boolean
True False
print, Your First Function in Python
• print is a built-in Function of Python.
• No need to build it. Just directly use.
• A Function is a command you use to tell Python what to do.
• You could directly use built-in Functions, or build Functions by yourself.
• print ( )
• For example:
• print() is a function. What you put into the (), is the
Parameter/arguments.
sep='separator' Optional. Specify how to separate the objects, if there is more than one. Default
is ' '
end='end' Optional. Specify what to print at the end. Default is '\n' (line feed)
flush Optional. A Boolean, specifying if the output is flushed (True) or buffered (False).
Default is False
Parameters of Print ()
sep='separator' Optional. Specify how to separate the objects, if there is more than one. Default
is ' '
end='end' Optional. Specify what to print at the end. Default is '\n' (line feed)
flush Optional. A Boolean, specifying if the output is flushed (True) or buffered (False).
Default is False
Parameters of Print ()
• Print three messages together, and specify the separator as
you wish:
?
input, Your Second Function in
Python
• print() is for transferring data from computer to the
user(terminal)
• input() is for data transferring data form the user to the
Developer
computer
User/
print() Terminal
input()
input, Your Second Function in Python
• A Function is a command you use to tell Python what to do. You could directly use built-in
Functions.
• input, as a Function, is to give the user a way to speak to the program by keying in text.
• The input() function pauses the program while it waits for the user to type some text.
Use input() to take user’s
• Its format is: input
• input ( “” )
• A English word (lowercase) A pair of parentheses (English pattern, no space)
• Put prompt into quotation mark. Prompt is the text we wish to display on the screen. It is optional.
• Then, run it.
• For example:
• And user can enter text after the prompt, in terminal, and then press Enter key.
input, Your Second Function in Python
• Most of the time we want to store the text that the user entered.
• So we need a variable (container) to store it as the data.
• How to do this?
• Variable, equal sign, input, paren, quotation mark, a prompt that prompts the
user to enter
• When python gets the user‘s input text, it stores the text the user
keyed in (as data) into the variable on the left side of the equal
sign.
• And then, we can use this variable.
Task:
• We need to get the user's name and then show a
greeting with a specific name on the screen.
Task:
• We need to get the user's name and then show a
greeting with a specific name on the screen.
show a sentence on the screen “Hello, XXX. Nice to meet you!” (XXX is the user’s
Flowchart help thinking
show a sentence on the screen “Hello, XXX. Nice to meet you!” (XXX is the user’s
Task:
• We need to get the user's name and then show a
greeting with a specific name on the screen.
show a sentence on the screen “Hello, XXX. Nice to meet you!” (XXX is the user’s
Flowchart help thinking
show a sentence on the screen “Hello, XXX. Nice to meet you!” (XXX is the user’s
Task:
• We need to get the user's name and then show a
greeting with a specific name on the screen.
show a sentence on the screen “Hello, XXX. Nice to meet you!” (XXX is the user’s
Flowchart help thinking
eate a variable to store the input and put this variable into the greeting sentence
show a sentence on the screen “Hello, XXX. Nice to meet you!” (XXX is the user’s
Task:
• We need to get the user's name and then show a greeting
with a specific name on the screen.
Use input().
Use print().
To show a sentence
Flowchart help thinking
Create a variable.
Use input().
Use print().
To show a sentence
Flowchart help thinking
Create a variable.
To
result store name and put name into the sentence
Use input().
Use print().
solutionUse input().
solutionUse print().
To show a sentence
Flowchart help thinking
solutionCreate a variable.
To
result store name and put name into the sentence
solutionUse input().
solutionUse print().
To
result store name and put name into the sentence
solutionUse print().
To
result store name and put name into the sentence
solutionUse print().
To
result store name and put name into the sentence
Thinking direction
solutionUse print().
To
result store name and put name into the sentence
solutionUse print().
or
Result in terminal:
AI assistant
• Please run above program in anaconda.cloud
• And seek its AI assistant’ help to deepen your
understanding of the program
• And get some advice from the AI to improve the
program
What about input numbers ?
• Use input(), the data we get is always string.
• But sometimes what we want to input is a number that
we can calculate.
• How to convert text into numbers?
Data Types
• Number type:
• Enter the number directly. E.g. 11334455
• We can do some calculations and show the results using the
print Function.
•+-*/
43
Convert Data Types
• Text type (string) →Number type (integer)
• int()
• str()
44
• print(type())
Task:
• Ask the user to enter his age and his brother's age. You
help him figure out how many years his brother is older
than him. Then you display the results on the screen.
Task:
• Ask the user to enter his age and his brother's age. You
help him figure out how many years his brother is older
than him. Then you display the results on the screen.
Task:
• Ask the user to enter his age, and then you help him
calculate the year he was born. Then you display the
results on the screen.
Task:
• Ask the user to enter his age, and then you help him
calculate the year he was born. Then you display the
results on the screen.
• Till now, what can we type in the Python by using VS code
as the IDE?
welcome sorry
Flowchart help thinking
welcome sorry
Flowchart help thinking
judgment
welcome sorry
Flowchart help thinking
judgment
welcome sorry
Flowchart help thinking
result
result judgment
judgment
judgment
judgment
judgment ???
• Format:
• Format:
Line 1 if condition colon
Next line indentation order
Next line else colon
Next line indentation order
indentation
else
indentation order
colon
•>
•<
• >=
• <=
• ==
• !=
Signs related to “=“, be careful!
• =
• Single equal sign
• Means “put into”
• Put data (on the right) into the variable (on the left)
• ==
• 2 equal signs
• Means “is equivalent to”
• Judge or claim
• It is used to determine whether the left and right sides of an equal sign
are exactly equal.
• !=
• Exclamation point and equal sign
Task:
• If a user tells you his name is Andy, then welcome him
and tell him he is allowed in.
• Users with other names should be told they are not
allowed in.
Flowchart help thinking
judgment ???
judgment if else
• In Python, elif is short for "else if" and is used when the
first if statement isn't true, but you want to check for
another condition. Meaning, if statements pair up with
elif and else statements to perform a series of checks.
• Only the block of code associated with the first true elif
condition encountered will be executed.
Try to use if-elif-else:
Task 1:
• The ticket policy for a park is as follows: