Download slides - McMaster Computing and Software

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ENGINEERING 1D04
Tutorial 1
WELCOME!
What we’re doing today
•
Who am I
•
How to do well
•
What are computer programs
•
Software Development Cycle
•
Pseudo Code vs. Code
•
Variables
•
Getting Input from a User
•
Python as a Calculator
•
Expressions/Literals
•
Comments
•
Print Statement
•
For loop/range function
•
Practice!
Important Info!
Who am I?
•
Rachel Lim
•
Just completed third year of Electrical
and Biomedical Eng
•
Have TAd for five terms so far, this is
my sixth
•
Love to travel
•
Email: [email protected]
How to Succeed
•
Practice, practice, practice!
•
Attend lectures, tutorials and labs
•
Practice before major labs
WHAT IS A COMPUTER
PROGRAM?
Think “Recipe”
Software Development Process
Analyze the
Problem
Maintenance
Determine
Specifications
Test/Debug
Create a
Design
Implement
the Design
Coding
•
Pseudo Code
•
•
•
•
•
•
Step 1: Gather Ingredients
Step 2: Preheat Oven to 350 F
Step 3: Mix Dry Ingredients
Step 4: Mix Wet Ingredients
Step 5: Combine Wet and Dry
Step 6: Bake for 20 minutes
•
Actual Code
•
•
•
•
•
•
oven = 350
wet = “milk” + “eggs”
dry = “flour + “sugar”
mix = wet+dry
cookies = string(oven) + mix
print cookies
Variables/Elements
•
A name for a space to hold information
•
The names are case sensitive
•
Should have some context
•
Names are called identifiers or variables
•
Must begin with a letter or underscore
Variables Names
•
Good
•
•
•
•
•
•
X
Temp
Count
Velocity
Speed
height
•
Bad
•
•
•
•
xhgjeo
!jigjrejgi
A,b,c,d,e,f,g (using letters as all of your
variable names can become confusing)
for, in, print (reserved words for
python – already have another
function)
Variable Types
•
int: plain integers > 3
•
long: long integers >3L
•
double: floating point numbers >3.0
•
complex: real and imaginary part numbers >3+4i
•
string: sequence of characters > hello
•
bool: Boolean (True or False)
•
type() will tell you what type of variable it is
Built in Functions
• abs() : gives you the absolute value of a number
• abs(-3) = 3
• round() : rounds a floating-point number to the nearest integer
• round(1.2) = 1
• int() and float() and string() will change what’s in brackets to its
own type
• int(1.2) = 1, float(1) = 1.0, string(1) = “1”
Assigning Variables
•
Correct format:
•
X=3
•
So the variable name will be on the LHS with the assigned value on the RHS after
an equal sign
•
You can also assign two values to two variables in one line:
•
x,y = 3,4
•
How would you swap two values?
•
x,y = y,x
Getting input from a user
• Say we would like to ask the user for a number
• We can use the function “input”
• Ex. x= input(“please enter a number: ”)
Using Python as a Calculator
•
+
•
-
•
/
•
%
•
*
•
**
Remember BEDMAS applies!
Integer Division
•
When you divide two integers, the answer can be given as a quotient and a
remainder
•
•
When Python divides, only the quotient will be given as the answer
•
•
Ex. 26/10 quotient: 2 remainder: 6
Ex. 26/10 = 2  This is known as Integer Division
How do we avoid this?
•
Make sure both numbers aren’t integers: 26., 26.0 and/or 10., 10.0
•
Ex. What will the result be if we type in 95/100?
Expressions and Literals
• Expressions: something that produces or calculates a
new value
• ie. x=3+5
• Literal: something that represents a specific value
• ie. 3
Comments
• Every time you code you should leave comments!
• Comments help other people understand your code
• In Python, comments begin with a #
• This means that Python will ignore anything coming after this
symbol
Print
•
We use print to output values onto the screen
•
Successive print statements will print on different lines
•
Blank print statements will print blank lines
•
You can use a comma to print multiple values on the same line
Definite Loop and Range Function
• A loop that will run for a specified number of times
• Takes the format for <var> in <sequence>:
<Body>
• Range is a function that will return a list of numbers
• REMEMBER! It always starts at zero
What will each do?
•
Python Demo
•
Range(6)
•
Range(1,6)
•
Range(4,-1)
•
Range(1,5,2)
Practice
•
Quadratic Formula
•
•
Solve: x2-x+6=0
And print it
Practice for Major 1
•
A torus is a donut-shaped solid made by forming a tube into a circle. The surface
area A of a torus with circle radius R and tube radius r is given by the following
formula:
•
•
A= 4∏2Rr
Requirements:
•
Program asks user to enter circle radius R and tube radius r, in that order
Program computes A and B, surface area of torus with R and 3r
•
Program outputs A, B and C where C = 100|
•
𝐴−𝐵
| in this order with appropriate labels
𝐵