Download presentation
Presentation is loading. Please wait.
1
Winter 2019 CISC101 4/8/2019 CISC101 Reminders TA s are listed on the “Labs” page of the course web site. More assignments are posted. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today Commanding the CPU – the use of a Stack.
Computer Languages – History of Python. Features of Python. Start Python Syntax by looking at Expressions. Winter 2019 CISC101 - Prof. McLeod
3
Last Time - Disassembly Experiment
Python has a module called “dis” that can show you the assembly language version of a Python line of code, function, object or an entire module. At the >>> prompt type: >>> import dis >>> a = 10 >>> b = 30 >>> c = 2 >>> dis.dis("x = a * b + c") Winter 2019 CISC101 - Prof. McLeod
4
Disassembly Experiment, Cont.
You will see: LOAD_NAME (a) 2 LOAD_NAME (b) 4 BINARY_MULTIPLY 6 LOAD_NAME (c) 8 BINARY_ADD 10 STORE_NAME (x) 12 LOAD_CONST (None) 14 RETURN_VALUE Winter 2019 CISC101 - Prof. McLeod
5
Disassembly Experiment, Cont.
The “stack” register is used to temporarily hold values and the results of calculations. How the stack changes after each instruction: top: 10 a 30 10 b 300 2 300 c 302 a * b + c 302 a * b x a * b a Winter 2019 CISC101 - Prof. McLeod
6
Aside – What’s a Stack? A “LIFO” – “Last In, First Out” type of data structure. (Like a plate dispenser in a cafeteria!) You “push” values on to the top of the stack and “pop” them off. What is a “stack overflow”? Often results from infinite recursion, for example. Winter 2019 CISC101 - Prof. McLeod
7
Machine Code Once code is disassembled, the interpreter needs to generate machine code. It uses the opcodes, opargs and operands from each line of assembly. We can view (and sometimes write) machine code in hexadecimal. But, the interpreter will not bother with this stage – it translates from assembly to binary. Now, the code is impossible for most humans to read! Winter 2019 CISC101 - Prof. McLeod
8
Compiled vs Interpreted
Binary commands can be compiled and then written to an *.exe or “executable” file. When you run a file like this the code is sent directly to the CPU, which is very fast! Python does not make these files, but generates binary machine language on the “fly” – sending it directly to the CPU, line by line. Python “interprets” source code, generates machine language and executes it immediately. Winter 2019 CISC101 - Prof. McLeod
9
Computer Languages: History
People became a bit frustrated with Assembler! The next generation of computer languages went up one more level, getting closer to something readable - for example: Fortran, Cobol and Lisp. These languages led to an explosion of over 200 languages being developed in the 60’s and 70’s, such as Basic, Pascal, C, Ada and Smalltalk. Python is a relative newcomer, arriving on the scene in the early 90’s. Winter 2019 CISC101 - Prof. McLeod
10
Aside – 99 Bottles of Beer…
See: Programs in 1500 different programming languages to generate the lyrics to the “song”. Winter 2019 CISC101 - Prof. McLeod
11
Aside - Malbolge A programming language designed to be extremely hard to program in and impossible to read. Named after the eighth circle of Hell in Dante’s “Inferno”. Featured in an episode of Elementary (S01E10). They supposedly had a program in Malbolge that de-crypted a safe’s combination. What they really had was “Hello World” in Malbolge: Winter 2019 CISC101 - Prof. McLeod
12
“Hello World” in Malbolge
Winter 2019 CISC101 - Prof. McLeod
13
Yikes! Python is much, much friendlier!! “Hello World” in Python:
print("Hello World") Winter 2019 CISC101 - Prof. McLeod
14
CISC101 History of Python The language was created by Guido van Rossum at Stichting Mathematisch Centrum in the Netherlands in the early 90’s. He is still very involved with the language and retains the title “BDFL”, which stands for “Benevolent Dictator for Life”. Python is named after “Monty Python”, not the snake!! Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
15
History of Python, Cont. He wanted to make the language:
an easy and intuitive language, but just as powerful as major competitors. open source, so anyone can contribute to its development . use code that is as understandable as plain English. to be suitable for everyday tasks, allowing for short development times. First released in 1994, the language was inspired by Modula-3, Lisp, SETL, Haskell, Icon and Java. A compilation of all the “Best-Of’s” from many other languages! Winter 2019 CISC101 - Prof. McLeod
16
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 2019 CISC101 - Prof. McLeod
17
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 2019 CISC101 - Prof. McLeod
18
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. Speed can be increased using byte-compiled files (like Java). Winter 2019 CISC101 - Prof. McLeod
19
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 2019 CISC101 - Prof. McLeod
20
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 2019 CISC101 - Prof. McLeod
21
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 2019 CISC101 - Prof. McLeod
22
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. Winter 2019 CISC101 - Prof. McLeod
23
Numeric Types in Python
CISC101 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
24
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 2019 CISC101 - Prof. McLeod
25
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 2019 CISC101 - Prof. McLeod
26
float Type For example to code the real number:
You would write: 2.43E-4 or 2.43e-4 exponent decimal Winter 2019 CISC101 - Prof. McLeod
27
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 2019 CISC101 - Prof. McLeod
28
Other Types What other types did you find? bool str
How about the collections? bool str list tuple dict set We’ll talk more about these later… Winter 2019 CISC101 - Prof. McLeod
29
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. Winter 2019 CISC101 - Prof. McLeod
30
From Exercise 1 – Literal Types
What are the types of the following literals? 45.237 3.4e-7 45e10 123 0b100101 0x45cde 'Hello!' "Again" """line1 Line2""" True [4, 6, 7.9, "Dingdong"] (4, 5, True) {'first':'Alan', 'last':'McLeod'} {3, 4, 7, 10} "H" 3.4E10 Winter 2019 CISC101 - Prof. McLeod
31
From Exercise 1 – Types, Cont.
How can you discover the type of a variable? How can you change a literal of one type to another? Winter 2019 CISC101 - Prof. McLeod
32
Variables What is a variable anyways?
CISC101 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
33
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 2019 CISC101 - Prof. McLeod
34
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 2019 CISC101 - Prof. McLeod
35
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 2019 CISC101 - Prof. McLeod
36
Aside – The Worst Variable Name!
My favourite bad variable name: l1 Winter 2019 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.