Week1 lessons
Week1 講的課程簡介和入門基礎,主要為三個部分,1、Introduction to the course(介紹課程)2、Revision(練習題)3、Tutorial: Unix and Python basics(課后輔導tut)
文章目錄
- Week1 lessons
- 前言
- 1. Lecture: Introduction to the course
- 1.1 Hello, world!
- 1.2 Echo the input
- 1.3 Compiler Quiz
- 2. Revision
- 2.1 Getting ready for Week 2
- 2.2 Middle input
- 2.3 Add special
- 2.4 Dice
- 2.5 New Box
- 3. Tutorial: Unix and Python basics
- 3.1 Python 3
- 3.2 Hello, world! (stdout / standard out)
- 3.3 Box
- 3.4 String Variables
- 3.5 Comments
- 3.6 Numbers and Variables
- 3.7 Functions
- 3.8 Inputs (stdin / standard in)
- 總結
前言
創作不易,拒絕抄襲,可以參考,標明出處, 小編會盡力去完善每一個知識點,如果有錯誤,漏掉的內容歡迎留言私信補充,內容說明:學校課件中會有一些PDF檔案,會單獨寫文章講解,1. Lecture: Introduction to the course
1.1 Hello, world!
Write a program that will print the output
Hello World!
print('Hello World!')
1.2 Echo the input
Write a program that will simulate an echo…that is, print the output of the user input.
user = input()
print(user)
1.3 Compiler Quiz
程式編譯
Q1:Is a logic error a computer mistake?
Answer: False
Explanation: All errors are human! but this one is caused by the human telling the computer to do the wrong thing!
Q2: Which of the following describes a compiler ?(多選)
Answer:
Checks syntax,
Producing a binary executable,
Converts human readable source code to mechanic code.
Explanation: A compiler does many things, but it’s main job is to convert human readable code to machine readable code.
Q3: Is compiling a program the same as running a program?
Answer :False
Explanation: Compiler is a step needed to allow the program to run on a computer. If the code does not compile.A compiled program can be run on the target machine it was compiled for.
2. Revision
2.1 Getting ready for Week 2
Write a program that outputs
"Getting ready for week 2!".
print('Getting ready for week 2!')
2.2 Middle input
Write a program that asks for three inputs, and print the second input.
Example:
$ python3 middle.p
y
a
b
c
Second input: b
middle1 = input()
middle2 = input()
middle3 = input()
print()
print('Seconde input: ' + middle2)
2.3 Add special
Write a program that asks for 3 inputs and output the sum of the 1st and 3rd input. You may assume the 1st and 3rd inputs will be integers.
Example:
$ python3 add_special.py
1
and
2
Output: 3
add_num = int(input())
add_word = input()
add_num2 = int(input ())
print()
print('Output: ' + str(add_num + add_num2))
2.4 Dice
Write a program that takes in 3 user inputs and prints the third given user input, followed by the sum of the first two user inputs.
Example:
$ python3 dice.py
Input: 1
Input: 2
Input: You rolled
You rolled 3
dice1 = int(input('Input: '))
dice2 = int(input('Input: '))
dice3 = input('Input: ')
print(str(dice3) +''+ str(dice1 + dice2))
2.5 New Box
Reproduce your answer to T1Q4, however ask for 3 inputs that represent the horizontal char, the vertical char and the corner char. Then print the box using these inputs.
Example:
hor = input('Horizontal: ')
ver = input('Vertical: ')
cor = input('Corner: ')
print()
print(cor + hor*8 + cor)
print(ver +' ' + ver)
print(ver +' ' + ver)
print(ver +' ' + ver)
print(cor + hor*8 + cor)
3. Tutorial: Unix and Python basics
3.1 Python 3
Python 2 was formally deprecated (i.e. unsupported) since the 1st of January 2020 and is not supported by Python 3. Because many systems were built with Python2 along with the fact that it isn’t very easy to upgrade every script to Python3, engineers were left with a tricky situation of how to use Python3 on a Python2 dependent system.
The workaround was to leave Python 2 named “python” in the terminal, and rename Python3 to"python3". This will only happen on systems that have Python2 installed already.
Since we will be using Python 3, you need to verify the version you are using.
Open a terminal and enterpython --version
- if you see
Python 3.x.xusepythonto run your scripts
else, if you seePython 2.x.x, try runningpython3 --version- if you see Python
3.x.x, usepython3to run your scripts- else: ask your tutor
(Bonus cookie: There’s a specific branch not specified in the control flow block above. Can you find it?)
3.2 Hello, world! (stdout / standard out)
Hello, world! is usually the first go-to program everyone writes, and for good reason! It introduces you to the basic syntax (grammar) of the language and also verifies that your system is set up correctly!
1.Open your favourite text editor and create a new file
2.Write the following code:
print("Hello, world!")
3.Save the file with the filenamehello.py. You have now just created a program calledhello.py!
4.Open the terminal and cd to where you saved the file.
5.Run your program in the terminal using the commandpython3 < program name >!
Your program willprint Hello, world! to standard out (which in this case is the terminal screen)!
print('Hello World!')
3.3 Box
You’re on your own for this one!
Write a program calledbox.pythat will print the following box to the screen:
Note: lines that start with a$represent a terminal command. i.e.python3 box.pyshould be typed into the terminal by YOU, and should not be outputted by your program!
print('+-------+')
print('| |')
print('| |')
print('| |')
print('+-------+')
3.4 String Variables
A variable is an imaginary storage box you can put information in.
A string is a data type that contains characters. They are usually surrounded in single quotes ’ ’ or double quotes " ".
In Python, there is no difference whether you use single or double quotes! (Other languages like C do have different uses for single and double quotes, so be careful!)
You can create a variable that stores a string like this:
first_name = "Santa"
last_name = "Claus"
Part 1
Create a new variable calledfull_namewhose value isfirst_name+last_nameand printfull_nameto the screen.
Hint 1:c = a + b
Hint 2:print(c)
first_name = ''
last_name = ''
full_name = first_name + last_name
print(full_name)
3.5 Comments
The code you write should be as self-documenting as possible. That means that the code you write should communicate what it is doing clearly. This can be done using proper naming, good spacing, and good structure.
However, there are times where the code cannot be very self-documenting. For example what does the following piece of code do? (it’s a bit beyond what you’ve done so far, but it’s good enough as an example)
IMPORTANT: Comments start with a#symbol.
PYTHON
#Take an item from the location
if user_ input_ splitted[0] == "take":
# Check item is in this location
item_ to_ be_ taken = get_ obj(user_ input_ splitted[1] , goose. location. items)
if item_ to_ _be_ taken == None:
print("You don't see anything like that here.")
else:
goose. take(item_ to_ _be_ taken)
goose. location. remove_ item(item_ to_ _be_ _taken)
print("You pick up the [].". format(item_ to_ _be_ taken. item_ name) )
chaser_ take_ action( )
It is absolutely crucial that you name your variables well and structure your code appropriately. Comments are an aid in readability and cannot be slapped onto bad code like a band aid.
Do not comment every line, but do comment the parts that that may be confusing! In assessments, there will be marks assigned to having comments in appropriate places.
3.6 Numbers and Variables
There are three types of Number data types in Python:
| Type of Number | Description |
|---|---|
| int | Interger, a whole number |
| float | Floating point, decimal numbers |
| complex | Describes imaginary numbers |
We won’t be dealing with complex numbers, but keep in mind that they exist.
Part 1:
- Create two variables called
aandband give them the starting values of1and2respectively.- Create another variable called
cand give it the value ofa + b. Print the value ofcto the screen. Is it what you expect?Part 2:
- Change the
+operator to the-operator- Print the value of
cto the screen. Is it what you expected?- Try the
*and/operators. What do they do?Part 3:
- Returning to [T1 Q5], what happens when you try to minus strings? (i.e.
first_name - last_name) Try reading the last line of the output and see if you can decipher what it is saying. Does it make sense to minus a string by another string?- What about the
*and/operators? (Actually try these, don’t skip this). Does it make sense to multiply or divide a string by another string?Part 4:
- Try to multiply a string with a number, e.g.
a = 'Hi!'
b = 5
print(a*b)
String multiplication is Python-only syntax. It is rarely supported in other languages and thus we recommend you NOT to use this functionality. In this course, we aim to teach you the essence of programming.
a = 1
b = 2
c = a + b
print(c)
3.7 Functions
A function is a block of organized, reusable code that is used to perform a single, related action.(source)
Here,int()is a function that converts a given string and returns (outputs) an integer.
print("Hello, world!")was also a function! Guess what it took in as an input!
Part 1:
Try the following piece of code. Is there a difference between the two print statements?
a = "Hello, world!"
print(a)
print("Hello, world!")
Part 2:
Try the following piece of code. What is the difference betweenaandb?
a = 1
b = "1"
print(a)
print(b)
Part 3:
Add the following line of code to Part 2:
print(a + b)
What does the error message say? (Hint: You cannot add an integer and a string! It makes no sense!)
Part 4:
Python provides two in-built functions calledint()andstr()which convert a given input into their respective data types.
- Continue from your code in part 3.
- Create a new variable called
cwhose initial value isabut converted to a string.- Run Part 3 again with
bandcnow. Do you see what you expect? What does it mean to add a string to a string? (Hint: this is called string concatenation)
Part 5:
- Continue from your code in part 4.
- Create a new variable called
dwhose initial value isbbut converted to an integer.- Run Part 3 again with
aanddnow. Do you see what you expect?What does it mean to add a number to a number? (not a trick question)
It is very important that you understand the differences between numbers and strings. In Python it is very easy to make these mistakes!
3.8 Inputs (stdin / standard in)
Now that we understand some basic ideas of programming, let’s spice up our programs to include user inputs.
Python has yet another in-built function called input(). It takes in a single string as an input, prints it to the screen, waits for the user to input something, and then returns (outputs) what the user typed in as a string.Part 1:
Write a program that performs exactly like the following:
$ python favourite_show.py
Hello!
What is your favourite show? Steins Gate
What type of show is it? Anime
Your favourite show, Steins Gate, is a Anime!
Part 2:
Write a program that performs exactly like the following:
$ python hello_there.py
Hello!
What is your first name? Naruto
What is your last name? Uzumaki
Nice to meet you, Naruto Uzumaki.
What is your age? #21
Amazing! Just another 79 years before you turn 100.
Note: Inputs are indicated with underline. These are things that the user types into the terminal!
Hint: You will need to convert the input into an integer for the age. A trick you can do to find the type a variable holds is to do:print(type(my_variable))(this is also called nested function calls!).type()is a function!
Hint 2: To add a string and an integer together, you’ll need to convert the integer into a string (refer to T1Q8)
Part 3:
Write a program that performs exactly like the following:
$ python fah_to_cel.py
What degrees in Fahrenheit would you like to convert to Celsius? 180
That is roughly equivalent to 82 degrees Celsius.
$ python fah_to_cel.py
What degrees in Fahrenheit would you like to convert to Celsius? 181
That is roughly equivalent to 83 degrees Celsius.
Hint: Use the round() function to round to the nearest integer.
Hint 2: Use Google to find the formula needed to do the conversion.
總結
這里對文章進行總結:以上就是今天要講的內容,本文僅僅簡單介紹了week1的大致內容,包括知識點概括,Quiz講解,一些難以理解的內容會中文講解,其余用英文解釋,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/250724.html
標籤:其他
上一篇:帶你一起學計算機專業英語!(IT行業、四六級黨快記起來)《軟體工程專業英語》第三單元:專案計劃——單詞、短語、名詞縮寫、難句


