程序代写代做代考 python 5AAVC210 Introduction to Programming WEEK 2

5AAVC210 Introduction to Programming WEEK 2

Variables
It is helpful to think of variables as containers that hold information.
A variable can hold numbers, text, etc.
A variable has a value. That value can change as calculations or operations are carried out in the program (hence the name, variable).

x =  “world”
print(“Hello ” + x)

x =  “hello ”
y = “world”

print(x + y)
Don’t forget to add a space,
otherwise your output will
be Helloworld
The + sign combines text and variables,
or adds the values of variables together.

Types of variable values: Strings
Strings are text surrounded by either single quotation marks, or double quotation marks.

‘hello’ is the same as “hello”

Types of variable values: numbers
int
Short for integer, these are whole numbers, e.g. 3. They can be positive or negative and can be any length.

float
These are numbers with decimal places, e.g. 3.8. They can be positive or negative and have one or more decimal places. They can also be scientific numbers with an “e” to indicate the power of 10.

complex
Advanced, for mathematics (i.e. when working with imaginary numbers). Ignore for now!

Casting
Sometimes you may want to turn one type of variable value into another.

We can do this by telling Python what to convert the values to, using int(), float(), and str().

Operators

Python Assignment Operators

Assignment operators are used to assign values to variables:

This is kind of a shortcut way of doing things

Types of variable values: boolean
The Python type for storing true and false values is called bool

There are only two boolean values. They are True and False. Capitalization is important, since true and false are not boolean values.

Note the lack of quotation marks! Boolean values are not strings!

Why is that useful?

Comparing values
x = 5
y = 3

print(x == y)

False

Here we declare the variables x and y and give them their values
Here we use the comparison operator == to see if x is equal to y
The code returns the value False because 5 is not equal to 3

Note the difference between
= (to assign a value)
and
== (to compare a value)

Functions
A function is a block of code which only runs when it is called.
You give a function data, known as parameters.
A function can return data as a result.

Python has some built-in functions, like print(), which we’ve already used.
You can tell a function by the parentheses () which indicate where you should put the parameters it uses.
print(“Inside these brackets are the parameters”)

Input: Python has a built-in function for taking input from a user

This is what we want our prompt to say
Here we are declaring a variable, called x, and the value of x is going to be whatever is input at the prompt
This is what I have typed in as input. It gets stored in the variable x
The final step is to print the string “Hello, ” followed by the value of the variable x (which is whatever has been input)

12

We can also write our own functions

In Python a function is defined using the def keyword:

When we want to use it , we call a function it by typing the function name followed by parenthesis
define
call

To let a function return a value, use the return statement

define a function called my_function
my_function has a parameter which is a variable called x
We are telling the function to return the value of x multiplied by 5
returned
results
my_function is called with the parameter 3, so x = 3. It’s then multiplied by 5 to get the result
my_function is called with the parameter 5, so x = 5. It’s then multiplied by 5 to get the result
my_function is called with the parameter 9, so x = 9. It’s then multiplied by 5 to get the result

Conditional statements: if
Sometimes we want to our code to do something if certain conditions are met.
We can evaluate the condition by using an if statement.

This whitespace indentation is IMPORTANT. You will get an error if it’s not there.

Conditional statements: elif
The elif keyword is a way of saying “if the previous conditions were not true, then try this condition”.

Conditional statements: else
The else keyword catches anything which isn’t caught by the preceding conditions.

if
else
elif

if
else
elif

Questions?