Winter 2019 CISC101 1/17/2019

Similar documents
Fall 2017 CISC124 9/16/2017

Welcome to Python 3. Some history

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Introduction to Programming

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

The current topic: Python. Announcements. Python. Python

Python Input, output and variables

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Introduction. A. Bellaachia Page: 1

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Introduction to computers and Python. Matthieu Choplin

Computer Components. Software{ User Programs. Operating System. Hardware

Text Input and Conditionals

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Today CISC124. Building Modular Code. Designing Methods. Designing Methods, Cont. Designing Methods, Cont. Assignment 1 due this Friday, 7pm.

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Introduction to programming with Python

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Programming Model 2 A. Introduction

CS558 Programming Languages

Welcome to Python! If you re the type of person who wants to know

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Unit 3. Constants and Expressions

CSC 533: Organization of Programming Languages. Spring 2005

Problem Solving With C++ Ninth Edition

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Senthil Kumaran S

CS 177 Recitation. Week 1 Intro to Java

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

CSI32 Object-Oriented Programming

Reversing. Time to get with the program

MEIN 50010: Python Introduction

Programming with Python

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

CMSC 201 Computer Science I for Majors

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Full file at

3. Simple Types, Variables, and Constants

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Final Labs and Tutors

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

INFS 214: Introduction to Computing

Programming: detailed instructions which tell the computer hardware what to do aka software Computer Science: the study NOT of computers, but of what

Java Fall 2018 Margaret Reid-Miller

Visual C# Instructor s Manual Table of Contents

Lecture C7: Assembly programming

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

Programming Fundamentals

Lecture 3. Input, Output and Data Types

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

The C++ Language. Arizona State University 1

CS558 Programming Languages

Practical Malware Analysis

Intro. Scheme Basics. scm> 5 5. scm>

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

CS 115 Lecture 4. More Python; testing software. Neil Moore

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

CIS192: Python Programming

Lecture 1. basic Python programs, defining functions

G Programming Languages - Fall 2012

Discovering Computers 2008

Variables and Data Representation

An Introduction to Python (TEJ3M & TEJ4M)

Programming Languages and Program Development

Fall 2017 CISC124 10/1/2017

UEE1302 (1102) F10: Introduction to Computers and Programming

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Chapter 1: Introduction to Computers and Programming

CS 415 Midterm Exam Spring 2002

Algorithms and Programming I. Lecture#12 Spring 2015

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

SCSP Programming Technique C

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Introduction to Python. Prof. Steven Ludtke

(0) introduction to the course. how to learn a programming language. (0) course structure

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Beyond Blocks: Python Session #1

Warmup : Name that tune!

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

What are compilers? A Compiler

CS 1301 Exam 1 Answers Fall 2009

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Chapter 5 Names, Binding, Type Checking and Scopes

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

CHAPTER 1: INTRODUCTION TO COMPUTERS AND PROGRAMMING. 1 Muhalim Mohamed Amin Faculty of

PROGRAMMING FUNDAMENTALS

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

CS311 Lecture: The Architecture of a Simple Computer

Part III Appendices 165

Transcription:

CISC101 Reminders Today TA emails are listed on the Labs page of the course web site. More assignments are posted. Commanding the CPU the use of a Stack. Computer Languages History of Python. Features of Python. Start Python Syntax by looking at Expressions. CISC101 - Prof. McLeod 1 CISC101 - Prof. McLeod 2 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") CISC101 - Prof. McLeod 3 You will see: Disassembly Experiment, Cont. 1 0 LOAD_NAME 0 (a) 2 LOAD_NAME 1 (b) 4 BINARY_MULTIPLY 6 LOAD_NAME 2 (c) 8 BINARY_ADD 10 STORE_NAME 3 (x) 12 LOAD_CONST 0 (None) 14 RETURN_VALUE CISC101 - Prof. McLeod 4 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 a * b 2 c 302 a * b + c 302 x 0 a 300 a * b 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. CISC101 - Prof. McLeod 5 CISC101 - Prof. McLeod 6 Prof. Alan McLeod 1

Machine Code Compiled vs Interpreted 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! 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. CISC101 - Prof. McLeod 7 CISC101 - Prof. McLeod 8 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. See: Aside 99 Bottles of Beer http://www.99-bottles-of-beer.net/ Programs in 1500 different programming languages to generate the lyrics to the song. Python is a relative newcomer, arriving on the scene in the early 90 s. CISC101 - Prof. McLeod 9 CISC101 - Prof. McLeod 10 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: Hello World in Malbolge ('&%:9]!~} z2vxwv-,poqponl$hjig%eb@@>}=<m:9wv6wsu2t nm-,jcl(i&%$#" `CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~ 4XzyTT43Qsqq(Lnmkj "Fhg${z@> CISC101 - Prof. McLeod 11 CISC101 - Prof. McLeod 12 Prof. Alan McLeod 2

Yikes! History of Python Python is much, much friendlier!! Hello World in Python: print("hello World") 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!! CISC101 - Prof. McLeod 13 CISC101 - Prof. McLeod 14 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! 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. CISC101 - Prof. McLeod 15 CISC101 - Prof. McLeod 16 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! 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). CISC101 - Prof. McLeod 17 CISC101 - Prof. McLeod 18 Prof. Alan McLeod 3

Hello World Questions 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. 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? CISC101 - Prof. McLeod 19 CISC101 - Prof. McLeod 20 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? CISC101 - Prof. McLeod 21 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. McLeod 22 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 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? CISC101 - Prof. McLeod 23 CISC101 - Prof. McLeod 24 Prof. Alan McLeod 4

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. float Type For example to code the real number: You would write: 2.43 10-4 or 0.000243 (We won t use the complex type much!) 2.43E-4 or 2.43e-4 decimal exponent CISC101 - Prof. McLeod 25 CISC101 - Prof. McLeod 26 Other Bases Other Types 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. What other types did you find? How about the collections? CISC101 - Prof. McLeod 27 CISC101 - Prof. McLeod 28 bool str list tuple dict set We ll talk more about these later 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. From Exercise 1 Literal Types What are the types of the following literals? 45.237 3.4e-7 45e10 123 0b100101 'Hello!' """line1 Line2""" 0x45cde "Again" True [4, 6, 7.9, "Dingdong"] (4, 5, True) {'first':'alan', 'last':'mcleod'} {3, 4, 7, 10} "H" 3.4E10 CISC101 - Prof. McLeod 29 CISC101 - Prof. McLeod 30 Prof. Alan McLeod 5

From Exercise 1 Types, Cont. Variables How can you discover the type of a variable? How can you change a literal of one type to another? 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. CISC101 - Prof. McLeod 31 CISC101 - Prof. McLeod 32 In code: Assigning/Creating a Variable 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. 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? CISC101 - Prof. McLeod 33 CISC101 - Prof. McLeod 34 Use descriptive names. Variable Naming Style Rules Capitalize successive words in a name using camelcase. Aside The Worst Variable Name! My favourite bad variable name: l1 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. CISC101 - Prof. McLeod 35 CISC101 - Prof. McLeod 36 Prof. Alan McLeod 6