tutorial 3 – concepts for a1 - university of calgary in...
TRANSCRIPT
![Page 1: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/1.jpg)
CPSC 231Introduction to Computer Science for
Computer Science Majors I
DANNY [email protected]
September 23, 2014
Tutorial 3 – Concepts for A1
![Page 2: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/2.jpg)
Agenda
script command more details Submitting using d2l Formula for weighted average Examples to help with assignment 1
![Page 3: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/3.jpg)
script Makes record of a terminal session. script [filename] Ex: 'script scriptfile' will record a script in a file called 'scriptfile' Default filename: typescript
Using backspace will create garbage characters in a script file
If you make an error just hit enter a few times to make a blank line
![Page 4: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/4.jpg)
Submitting Using D2L
![Page 5: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/5.jpg)
Submitting Using D2L
![Page 6: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/6.jpg)
Submitting Using D2L
![Page 7: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/7.jpg)
Submitting Using D2L
![Page 8: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/8.jpg)
Submitting Using D2L
![Page 9: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/9.jpg)
Submitting Using D2L
![Page 10: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/10.jpg)
Submitting Using D2L
![Page 11: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/11.jpg)
Calculating Weighted Average
l 6 mini assignments (1% each for 6% total)l 5 assignments (4%, 5%, 6%, 7%, 7% for a total of 29%)
l 2 exams (25% midterm, 40% final for a total of 65%)
![Page 12: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/12.jpg)
Calculating Weighted Average
l Suppose a student got 3.0 for all assignments, 2.7 for midterm and 2.0 for final
l (3.0 * 0.06) + (3.0 * 0.29) + (2.7 *0.25) + (2.0 * 0.40) = 2.525
l Don't forget to round to two decimal places, so 2.525 would be rounded to 2.53
![Page 13: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/13.jpg)
Formatting
l print("%5.2f %-5d " %(1.599, 23))l %5.2f indicates that the floating point number passed in will be printed with a left aligned width of at least 5 characters (including the '.') and have 2 places of precisionl If a number shorter than 5 digits is passed in (23.25) then leading spaces will be added to create a left aligned width of at least 5 characters
![Page 14: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/14.jpg)
Formatting
l print("%5.2f %-5d " %(1.599, 23))l %-5d indicates that the integer passed in will be printed with a right aligned width of at least 5 charactersl If an integer of less than 5 digits is passed in trailing spaces will be added to create a right aligned width of 5 spacesl
![Page 15: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/15.jpg)
Variables
l Variables are essentially named containers used to store data which can come in various types
l Data can come in the form of integers, floats, and strings
l Integers are non decimal numbers (1, 3, 20, ...)l Floats are decimals numbers (1.0, 3.0, 9.42, ...)l Strings are any sequence of characters (cat, dog, fgd) they do not have to be actual words
![Page 16: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/16.jpg)
Naming Variables
l Make sure the name is meaningfull In Python the name must begin with a letterl Names are case sensitive but avoid distinguishing two variables by case
l Names should be all lower case when dealing with a single word name (width = 2)
l With multiple word names distinguish words by capitalizing (ballWidth = 2)
![Page 17: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/17.jpg)
Naming Variables
l Names cannot be Python keywords
![Page 18: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/18.jpg)
Constants
l Constants are variables whose values don't change
l Unnamed constants, often called magic numbers, are numerical values that appear in programs (num = num + 12, here 12 is an unnamed constant)
l Named constants are values that are referred to by a name ( PI = 3.14)
l Avoid using unnamed constants unless it is for simple values such as 2 in circumference = 2 * PI * r
![Page 19: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/19.jpg)
Operators
l Python has basic operators such as *, +, and -l Division is slightly more complex, when dividing two numbers such as 9/4 you will get a float result
l Integer division using two back slashes gives you an integer result, 9//4 = 2
l Modulo gives you the remainder of an integer divison, 9%4 = 1
![Page 20: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/20.jpg)
Common Syntax Errors
l Remember to match quotes of the same type, “ wont match with '
l Remember to declare a variable before using is, before typing print(num) make sure num has been declared
![Page 21: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/21.jpg)
Escape Codes
l \t is the sequence for a tabl \n is the sequence for a newlinel To print characters such as '\' and ' “ ' we need to provide the escape character '\'l Print(“\””) will print “l Print(“\\”) will print \
![Page 22: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/22.jpg)
Order of Operation
l Num = 2 + 2 * 3 / 2l print(num) will print 5
l Num = (( 2 + 2) * 3) / 2l print(num) will print 6
![Page 23: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/23.jpg)
Types
l Num = 12 + 12l print(num) will print 24
l Num = '12' + '12' (String concatenation)l print(num) will print '1212'
l To convert between types use int(), float(), and str()l int('12') + int('12') = 24l float(12) + float(12) = 24.0l str(12) + str(12) = '1212'
![Page 24: Tutorial 3 – Concepts for A1 - University of Calgary in ...kremer.cpsc.ucalgary.ca/courses/cpsc231/tutorialNotes/Week-2-1-Assn1.pdfCPSC 231 Introduction to Computer Science for Computer](https://reader033.vdocuments.mx/reader033/viewer/2022053117/609b2ce41dbb2d449440b856/html5/thumbnails/24.jpg)
User Input
l To get user input use the input() functionl Input() returns a string so don't forget to convert the input you have obtained from the userl num = input(“enter your age: “) means num will contain
a string, use int() to convert