Week1 讲的课程简介和入门基础,主要为三个部分。1、Introduction to the course(介绍课程)2、Revision(练习题)3、Tutorial: Unix and Python basics(课后辅导tut)
Write a program that will print the output
Hello World!
print('Hello World!')
Write a program that will simulate an echo…that is, print the output of the user input.
user = input()
print(user)
程序编译
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.
Write a program that outputs
"Getting ready for week 2!"
.
print('Getting ready for week 2!')
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)
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))
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))
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.
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)
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.x
usepython
to run your scripts
else, if you seePython 2.x.x
, try runningpython3 --version
- if you see Python
3.x.x
, usepython3
to 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?)
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!')
You’re on your own for this one!
Write a program calledbox.py
that will print the following box to the screen:
Note: lines that start with a$
represent a terminal command. i.e.python3 box.py
should be typed into the terminal by YOU, and should not be outputted by your program!
print('+-------+')
print('| |')
print('| |')
print('| |')
print('+-------+')
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_name
whose value isfirst_name
+last_name
and printfull_name
to the screen.
Hint 1:c = a + b
Hint 2:print(c)
first_name = ''
last_name = ''
full_name = first_name + last_name
print(full_name)
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.
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
a
andb
and give them the starting values of1
and2
respectively.- Create another variable called
c
and give it the value ofa + b
. Print the value ofc
to the screen. Is it what you expect?Part 2:
- Change the
+
operator to the-
operator- Print the value of
c
to 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)
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 betweena
andb
?
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
c
whose initial value isa
but converted to a string.- Run Part 3 again with
b
andc
now. 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
d
whose initial value isb
but converted to an integer.- Run Part 3 again with
a
andd
now. 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!
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讲解。一些难以理解的内容会中文讲解,其余用英文解释。