系列笔记-USYD悉尼大学INFO1110 ED lessons Week1 课件 作业 assignment讲解

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:
系列笔记-USYD悉尼大学INFO1110 ED lessons Week1 课件 作业 assignment讲解_第1张图片

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 enter python --version

  • if you see Python 3.x.x use pythonto run your scripts
    else, if you see Python 2.x.x, try running python3 --version
  • if you see Python 3.x.x, use python3 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?)

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 filename hello.py. You have now just created a program called hello.py!

4.Open the terminal and cd to where you saved the file.

5.Run your program in the terminal using the command python3 < program name >!

Your program will print 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 called box.py that will print the following box to the screen:
系列笔记-USYD悉尼大学INFO1110 ED lessons Week1 课件 作业 assignment讲解_第2张图片
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('+-------+')

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 called full_name whose value is first_name + last_name and print full_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)

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 a and b and give them the starting values of 1 and 2 respectively.
  • Create another variable called c and give it the value of a + b . Print the value of c 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)

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 between a and b ?
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 called int() and str() 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 is a but converted to a string.
  • Run Part 3 again with b and c 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 is b but converted to an integer.
  • Run Part 3 again with a and d 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!

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讲解。一些难以理解的内容会中文讲解,其余用英文解释。

你可能感兴趣的:(悉尼大学,info1110,python,恰饭,经验分享)