CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Computer Science 1620 Loops.
Recursion. Binary search example postponed to end of lecture.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CIT 590 Intro to Programming First lecture on Java.
Games and Simulations O-O Programming in Java The Walker School
Simple Python Loops Sec 9-7 Web Design.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Intro to Programming Lecture 13
Introduction to Python
Programming for Linguists An Introduction to Python 24/11/2011.
Lists in Python.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
CIT 590 Examples Debugging. Agenda This class is an experiment in letting students learn by just doing things as opposed to me talking all the time! So.
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
CIT 590 Intro to Programming First lecture on Java.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
I Power Higher Computing Software Development High Level Language Constructs.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Getting Started With Python Brendan Routledge
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
String and Lists Dr. José M. Reyes Álamo.
Topic: Iterative Statements – Part 1 -> for loop
Introduction To Repetition The for loop
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Programming for Engineers in Python
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
More Selections BIS1523 – Lecture 9.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Coding Concepts (Basics)
IST256 : Applications Programming for Information Systems
String and Lists Dr. José M. Reyes Álamo.
Class code for pythonroom.com cchsp2cs
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Presentation transcript:

CIT 590 Intro to Programming Lecture 4

Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity! Snowman example Lists Please note that we are not covering recursion (yet!)

How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’ ‘lists’ If it is a mathematical/scientific program we will care more about correctness. A math error in HW1 is more pardonable than one in HW2 If it is a game, the fun element is important. If it is a user interface (we’ll have one later) – make it look nice! Pay a ton of attention to function names and specs Style is hard to define but you know bad style when you see it Plagiarism is often detectable and will be dealt with harshly Do not post your question on stack overflow!

Proper way to end your python file if __name__ == “__main__”: starting_function() When the program runs, it will begin at starting_function() Advantages of doing this Code will run automatically when you hit Run Your unit tests (coming up shortly) will not execute the whole program.

Break, quit, exit How to stop a loop? YOU WILL NOT LOSE POINTS IF THIS IS A MISTAKE YOU MADE Quit() is a bad bad idea. Quit exits the python process. You only wanted to end your program. You did not want to end Python. Sys.exit() also a bad idea That quits the execution of the program and that means that subsequent commands are not executed break is good but can make code hard to read when code is long The ideal way is to set a boolean

functions and argument passing Options for question 1 are A) 4 B) 8 C) -8 D) -4 E) Error of some sort

Scope Options for question 2 are A) 4 B) 8 C) -8 D) -4 E) Error of some sort

Functions, slicing, scope Options for question 3 are A) 92 B) 90 C) 3 D) 1 E) 5

Modularity Snowman example HW2 focus is on modularity Not a hard HW but you should be writing more functions than the ones prescribed in the assignment Do not worry about the m

lists ls = [1, 4, 5] Very similar to strings in terms of some of the basic operations Slicing is your best friend What is [2, 3, 4] + [7, 8, 9]? ls * 3 will repeat the list 3 times List functions Append – add elements Extend - add this other list at the end Index - find and return index Count – count the number of occurrences of an element

lists Iterating over the elements of a list for listelement in lst: Python has no issue with mixed types >>> [1, 0.45, True, 'abc', [1,2,5]] [1, 0.45, True, 'abc', [1, 2, 5]]

List functions work by side effect ls.append() automatically changes the value of ls The function append actually returns the value ‘None’, so if you were to do ls = ls.append(5) or ls = ls.extend([5, 6, ‘t’]) Disaster! >>> lst = [45, '6', True] >>> lst.append(lst.append(576)) What will print lst give me now?

Fixed sized lists (arrays?) You can initialize a fixed length list by doing something like [None] * 10 [0] *5 Fixed length lists behave very similar to the concept of arrays found in other programming languages

Range generates a list Range basically produces a list range(1,10) 1 through 9 range(1,17,4) – in steps of 4 [1, 5, 9, 13] xrange versus range xrange is more memory efficient but does not give you an actual list

Assignment by reference as opposed to value(copy) A variable is always assigned by reference in Python If you’ve seen pointers in C, this is equivalent to that concept You can make a true copy by doing a complete slice Function arguments are also passed by reference Be very careful when doing this See sneakyAppend for an example

Identity and equality Use the identity testing operator ‘is’ if you want to ensure two variables hold references to the same object Use ‘==‘ operator if you want to ensure that the two variables have the same value, even if they are not the same object a is b implies a == b, but not necessarily the other way around