Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.

Similar presentations


Presentation on theme: "Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals."— Presentation transcript:

1 Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals (“if” statements) are not on the quiz. Exercises 1 and 2 are fair game. You will need to understand conditionals for the assignment. 45 minutes on paper – no aids. Pen or pencil. First part of lab. Tell your TA if you cannot write in the first hour so he or she will expect you later in the lab. T/F, short answer, read code (“what is the output?”), write code (obtain something, calculate something else, display result). Returned in class, marks in Moodle. Winter 2016CISC101 - Prof. McLeod1

2 Winter 2016CISC101 - Prof. McLeod2 Today Features of Python. “Hello World” in a function definition. A program “template”. Python expressions.

3 CISC101 - Prof. McLeod3 Features of Python High Level –Most notable are the built-in data structures. Object Oriented –OOP helps you to build code in a modular way. But, Python allows you to write code without knowing anything about OOP! Scalable –Packaging of code allows even very large programming projects to be manageable. Extensible –You can easily use external code modules written in Python, C, C++, C#, Java or Visual Basic. Winter 2016

4 CISC101 - Prof. McLeod4 Features of Python, Cont. Portable –Runs on any platform/OS combination that can run C. Easy to Learn (!) –Relatively few keywords, simple language structure and clear syntax. OOP can be avoided while learning. Easy to Read –Much less punctuation than other languages. Forces you to use good indentation. Easy to Maintain –Results from the two above features! Winter 2016

5 CISC101 - Prof. McLeod5 Features of Python, Cont. Robust –Exception handlers and safe, sane and informative crashes. Good for Rapid Prototyping –Often used with other languages to create prototypes and provide a testing platform. Built-In Memory Management –Like Java. Avoids a major problem in C/C++. Interpreted –Not compiled – each command is executed as it is read from the program. Winter 2016

6 Hello World Write a program that prompts the user for his first name and then displays “Hello name” to the console. Put the code in a main function. Just for fun, disassemble the program when it is done. Winter 2016CISC101 - Prof. McLeod6

7 Questions Do you have to put code in a main function? Why is it called “main()”? What’s with the “()”? They are empty! Why is there a call to main() at the end of the program? How can you tell what code is inside main() and what is outside? Winter 2016CISC101 - Prof. McLeod7

8 Questions, Cont. What does the input() BIF return? How can you get a number, like an int, from input()? What does the print() BIF return? What does “+” do with strings? I can add numbers with “+”, as well. Can I add a number to a string? Winter 2016CISC101 - Prof. McLeod8

9 Python Expressions A program is a series of expressions, one per line. An expression is built from one or many of the following pieces: –Literal values –Variables –Keywords –Function and method calls –Punctuation “Syntax” supplies the rules about how these pieces go together so the interpreter can understand our commands. CISC101 - Prof. McLeod9Winter 2016

10 CISC101 - Prof. McLeod10 Numeric Types in Python In Exercise 1 you (have or will?) discovered that literal values can be of different types. What numeric types did you find? int float complex Winter 2016

11 CISC101 - Prof. McLeod11 Numeric Types, Cont. Each type’s literal value is characterized by the way it is typed into a program. Do you remember the characteristics of each type? How does the interpreter tell them apart? Winter 2016

12 CISC101 - Prof. McLeod12 Numeric Types, Cont. The int type is an integer (no decimal or exponent) and there is no limit to its size. The float type is characterized by a decimal place and/or an exponent. It is limited to about 17 digits. (We won’t use the complex type much!) Winter 2016

13 float Type For example to code the real number: 2.43 × 10 -4 or 0.000243 You would write: 2.43E-4 or 2.43e-4 CISC101 - Prof. McLeod13 decimal exponent Winter 2016

14 CISC101 - Prof. McLeod14 Other Bases Normally we view numbers in base 10, or in a “radix” of 10. That’s the default in Python. How can you create literal numbers in base 2, 8 or 16? Use the prefixes: 0b, 0o or 0x on literals. Use the BIFs: bin(), oct() and hex() to display a value in another base. Winter 2016

15 CISC101 - Prof. McLeod15 Other Types What other types did you find? How about the collections? bool str list tuple dict set We’ll talk more about these later… Winter 2016

16 Python Types, Cont. Python determines the type of a literal value by examination. When the value is assigned to a variable, the variable is typed to match the type of the value. Python is a dynamically typed language. Which means that a variable can change types. CISC101 - Prof. McLeod16Winter 2016

17 From Exercise 1 – Literal Types What are the types of the following literals? Winter 2016CISC101 - Prof. McLeod17 45.2373.4e-7 45e10123 0b1001010x45cde 'Hello!'"Again" """line1 Line2""" True [4, 6, 7.9, "Dingdong"] (4, 5, True) {'first':'Alan', 'last':'McLeod'} {3, 4, 7, 10} "H"3.4E10

18 CISC101 - Prof. McLeod18 Variables What is a variable anyways? In Python, variables are created by an assignment statement (or in function parameter lists). A variable takes the type of the value being assigned to it when the program runs. A variable’s value can change any time, as can its type. Winter 2016

19 CISC101 - Prof. McLeod19 Assigning/Creating a Variable In code: myVal = 20 Now myVal refers to some location in RAM that stores the int type value 20. We don’t have to worry about what the actual memory address is. Winter 2016

20 CISC101 - Prof. McLeod20 Variable Naming Syntax Rules Variable names are case sensitive. You can’t use a Python keyword for a variable name. No spaces! Start with a letter (use lower case, by convention), or an underscore _. The rest of the name can contain numbers, letters or the underscore (no spaces – oops I said that already!) Why no spaces, anyways? Winter 2016

21 CISC101 - Prof. McLeod21 Variable Naming Style Rules Use descriptive names. Capitalize successive words in a name using camelCase. No limit to the length of a variable name, but don’t write an essay!! Don’t use single letter variable names, except if you need a loop counter that has no intrinsic meaning, then you can use i, j or k. Winter 2016


Download ppt "Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals."

Similar presentations


Ads by Google