CS1022 Computer Programming & Principles

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

4 Control Statements: Part 1.
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Chapter 5: Control Structures II (Repetition)
Chapter 6 Writing a Program
Lists, Loops, Validation, and More
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Introduction to Programming
Formal Models of Computation Part II The Logic Model
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Chapter 7: Arrays In this chapter, you will learn about
Chapter 11: Models of Computation
Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.
Lilian Blot TO PROGRAMMING & PYTHON Introduction Autumn 2012 TPOP 1.
Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
CMPT 120 Control Structures in Python
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Lecture 7: Software Design (Part II)
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Decisions If statements in C.
25 seconds left…...
Chapter Three Arithmetic Expressions and Assignment Statements
Programming Fundamentals 1 st lecture. ELTE 2/ Content  Steps of solving problems – steps of writing a computer.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Pointers and Arrays Chapter 12
Python Basics: Statements Expressions Loops Strings Functions.
Introduction to Programming G51PRG University of Nottingham Revision 1
Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development.
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Chapter 8 Improving the User Interface
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Introduction to Python
Chapter 2 Writing Simple Programs
CS1022 Computer Programming & Principles
Python.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
Python Let’s get started!.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Chapter 2 Writing Simple Programs
CS1022 Computer Programming & Principles
CS1022 Computer Programming & Principles
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Variables, Expressions, and IO
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders All assignments are now posted.
Presentation transcript:

CS1022 Computer Programming & Principles Lecture 2.1 A brief introduction to Python (1)

Plan of lecture Motivation From pseudo-code to Python Variables in Python Input statements Assignment statements Formatting your program Conditional statement Loops Instructions for next lecture CS1022

Motivation Pseudo-code is for humans “Running” pseudo-code: time-consuming & error-prone Solution: use a programming language We can “implement” pseudo-code Easily run the program and see whether it works. Various choices of programming languages: Java C, C++ and C# Visual Basic Haskell and Scala Any others? CS1022

Motivation (2) A family tree of programming languages: CS1022

Motivation (3) We’ll be using Python as a programming language We chose Python for various reasons: Widely used, general-purpose and high-level Emphasises code readability Compact programs (fewer lines of code) Simple yet powerful Multi-paradigm: object-oriented and functional Well supported (tutorials, free books, etc.) Growing community of users CS1022

From pseudo-code to Python The pseudo-code control structures have Python counter-parts Some of them are quite close to each other Let’s look at them, one by one We’ll use this kind of font (teletype) to write Python code Indicates it’s something typed onto a computer Generally accepted convention in books, Web pages, etc. CS1022

x y12 a_variable valueOfPurchase Variables in Python Strings of letters, numbers and “_” (underscore) Must start with a letter Examples: x y12 a_variable valueOfPurchase Important: variables names are case-sensitive “numCreds” is not the same as “numcreds” Some strings are reserved words for Python Python needs these to make sense of your program You should not use these reserved words as variables CS1022

Do not use any of these strings as variable names!! Variables in Python (2) Reserved words in Python: and as assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while with yield Do not use any of these strings as variable names!! CS1022

Input statements Pseudo-code: input Var1, Var2,..., Varn Python: var_1 = argv[1] var_2 = argv[2] ... var_n = argv[n] Values from keyboard, Web form, database, etc. Get values from keyboard Assign them to variables In this order CS1022

Assignment statements Pseudo-code: Variable := Expression where Variable is any variable name Expression is any arithmetic expression Python: Variable = Expression Variable is any Python variable name (see above) Expression is any Python expression CS1022

Assignment statements (2) Sample assignment statements in Python: value = arg[1] date = '12/03/2034' expr = value * 2 value = value + 1 y = f(value) X = Y = 20 CS1022

Assignment statements (3) Wrong assignment statements in Python: myString = 'pugs are cool if = '12/03/2034' expr+2 = value * 2 CS1022

Back to variables Variables have associated types: Boolean Numeric Integer Float Long Complex Strings Some built-in functions require specific types Arithmetic operators with numbers If you try to use an operator with the wrong kind of variable Python will complain about it CS1022

Example of a Python program Example of algorithm with assignment statement: {Algorithm to add two numbers} begin input First, Second Sum := First + Second output Sum end # Program to add 2 numbers First = argv[1] Second = argv[2] Sum = First + Second print(Sum) CS1022

Formatting your program Spaces and line breaks are important in Python In some languages (Java, C) these only separate things In pseudo-code these help visualise constructs Indentation define “begin ... end” : if n < 0 then begin abs := –n; x := x + 1; end if n < 0: abs = -n x = x + 1 CS1022

Formatting your program (2) Nesting of statements: begin statement 1; statement 2; statement 3; statement 4; statement 5; end Algorithm statement1 statement2 statement3 statement4 statement5 Python CS1022

Conditional statement (if-then-else) Algorithmic notation if condition then statement 1 else statement 2 if condition: statement1 else: statement2 Python CS1022

Conditional statement (2) {Absolute value of input} begin input n; if n < 0 then abs := –n; else abs := n; output abs; end Algorithm # absolute value n = int(argv[1]) if n < 0: abs = -n else: abs = n print(abs) Python CS1022

“For” loop for var := init_val to final_val do Algorithmic notation statement for var in range(init_val,final_val): statement Python CS1022

“For” loop (2) Algorithm Python {Sum first n integers} begin input n; for i := 1 to n do sum := sum + i; output sum; end Algorithm # sum first n integers n = int(argv[1]) sum = 0 for i in range(1,n): sum = sum + i print(sum) Python CS1022

“For” loop (3) for all elements of set do Algorithmic statement notation for w in set: statement Python Important: Sets are represented as [1, a, ‘cat’, 12.3, 5] Square brackets, items separated by commas Sets may contain elements of different types CS1022

for var in range(initial,final,increment): “For” loop (4) Some issues with for-loops Is the final value still part of the loop? What is the final value of n below (initialised as 0)? for i in range(1,100): n = n + 1 Loop performed up to 99, but not with 100 Other languages are different (e.g., Java) Full syntax is for var in range(initial,final,increment): Example: range(100,1,-1) is count down CS1022

“While” loop while condition do Algorithmic notation statement Python CS1022

“Repeat-until” loop repeat Algorithmic statement notation until condition Algorithmic notation while True: statement if condition: break Python There is no built-in “repeat-until” in Python Re-purpose “while” loop for a similar effect CS1022

Instructions for next lecture Download “PortablePython_2.7.6.1.exe” from http://portablepython.com/wiki/PortablePython2.7.6.1/ Save it locally (on a USB or on your laptop) Run it (click on it) Choose somewhere to install it (it takes awhile...) You should see the following contents CS1022

Instructions for next lecture (2) Please make sure you follow the instructions We won’t have time to do them tomorrow You will be left out if you try to do them tomorrow We’ll have a “hands-on” session next Explore Python integrated development environment Write Python programs Make mistakes and learn with them Learn how to use a new tool and technology Get new skills Have fun breaking stuff CS1022

Summary Basics of Python programming Equivalence of algorithm & Python constructs An “entry-point” to learn more about Python CS1022

Further reading Python’s official Web site https://www.python.org/ Wikipedia’s entry on Python http://en.wikipedia.org/wiki/Python Codecademy on-line Python training http://www.codecademy.com/en/tracks/python Python Humour! (who said we are not fun/funny?) https://www.python.org/doc/humor/ Portable Python http://portablepython.com/ CS1022