Download Introduction to Computers & Programming: Review for Summer Midterm and more Study notes Algorithms and Programming in PDF only on Docsity!
Intro to: Computers & Programming Defining Identifiers: Objects with Names Adam Meyers New York University
Introduction to:
Computers & Programming:
Review for Summer Midterm
Intro to: Computers & Programming Defining Identifiers: Objects with Names
Summary
- Some Procedural Matters
- Summary of what you need to Know
- For the Test and To Go Further in the Class
- Two Practice in-class writing of programs
- I will give everyone 15-25 minutes to write the same programd independently
Intro to: Computers & Programming Defining Identifiers: Objects with Names Type of Questions that Could Be on the Test
- Section I: Answer questions about Code
- What is printed out?
- What is the value of a variable after code executes?
- Section II: Find and correct errors in code
- Identify errors
- State how to correct the errors
- Note: This type of question will not be on the final.
- Section III: Write functions that solve simple
problems involving: user input, printing,
calculating values, etc.
Intro to: Computers & Programming Defining Identifiers: Objects with Names What you should know
- Topics: algorithm, program, programming language, function, operator, input, output, side effect, variable, scope, data types, simple for loops, etc.
- (^) Know how to:
- Define functions
- Assign values to variables
- Return values from functions
- Use print statements, input statements, operators
- Import modules
- Use data types and coerce one data type to another
- Write a simple function that works and is easy to understand, due to comments and variable/function names
- If/elif/else statements and basic decision trees
- Create graphics with the turtle module
- Basic for loops
Intro to: Computers & Programming Defining Identifiers: Objects with Names
More on Algorithms
- Pseudo Code: a series of ordered statements
- Structured using line numbers, indents, bullets, etc.
- Connected by logical and temporal connectors
- if, else, unless, not, until, when
- Flow Chart: Connected Series of Boxes
- circles/ovals = start/end
- Rectangles = steps in processing
- Diamonds = Decisions
- Arrows = Sequence of Steps
Intro to: Computers & Programming Defining Identifiers: Objects with Names
Functions
- Programming language Functions have 3 optional features: - Input - Output - Side effects
- print versus return
- print is significant for its side effectโprinting to the computer screen
- return
- Exits block (function)
- Provides a value to a function call โ Example:
- If: function1(a) returns 5 and function2(b) returns 10
- Then: function(a) + function(b) = 15
Intro to: Computers & Programming Defining Identifiers: Objects with Names
String Components
- Two identical delimiters: โ, ', '''
- The Characters between delimiters including:
- digits (0-9)
- letters(a,b,c,..z,A,B,C,...,Z, space)
- escape characters: \n, \t, '
- Sample Strings:
- '!@##$^&*())'
- โThis is an apostrophe: ' โ
- 'This is a double quote: โ '
- (^) '''Triple quotes can include ' or โ or between them'''
Intro to: Computers & Programming Defining Identifiers: Objects with Names
More on Strings
- When working with strings they are
represented so it is easy to see all their
components:
- 'The quote ' and the newline \n are useful'
- When printed, a string will be displayed in a
way that interprets these components.
- The quote ' and the newline are useful
- The delimiters are eliminated and escape characters are interpreted.
Intro to: Computers & Programming Defining Identifiers: Objects with Names
Type Conversion Functions (Numbers)
- Float
- Converts Integers and compatible strings to floats
- Int
- Converts floats (by truncation) to integer
- Converts compatible string to integer
- Converted Strings can participate in math operations
- 5 * int('5')
- 20 / float('5.5')
Intro to: Computers & Programming Defining Identifiers: Objects with Names
Converting Non-Strings to Strings
- str(5.55)
- Makes a string out of any type of object
(using definition of that object)
- Once converted, non-strings can be
combined with strings through concatenation
- 'The number is '+str(5)
- output = 5+
- (^) 'The sum of 5 and 100 is '+ output
Intro to: Computers & Programming Defining Identifiers: Objects with Names
Boolean Operators
- and, or, not
- True and True โ True, True and False โ False, False and True โ False, False and False โ False
- not(True) โ False, not(False) โ True
- False or False โ False, True or False โ True, False or True โ> True, True or True โ True
- <, >, <=, >= โ Expected meanings from math
- ==, != โ 'is equal to', 'is not equal to'
- in โ currently, we only see this in for loops, but it is a Boolean operator that tests for membership, e.g., - 'a' in 'abcdefg' โ True, 5 in range(4) โ False
Intro to: Computers & Programming Defining Identifiers: Objects with Names
Making Code Readable
- Comments
Know How to use comments
Know Why to use comments
- Naming Variables and Functions
- Choosing names that are self explanatory
Intro to: Computers & Programming Defining Identifiers: Objects with Names If/elif/Else Statements
- Syntax (elif and else parts optional) If + boolean-expression + : body elif + boolean-expression + : body else: body
- Example: def classify_integer(integer): if integer==0: return('zero') elif (integer%2) == 0: return('even') else: return('odd')
Intro to: Computers & Programming Defining Identifiers: Objects with Names
Basics of Turtle Graphics
- Loading module, creating a screen and a turtle import turtle my_screen = turtle.Screen() my_turtle = turtle.Turtle()
- Basic Turtle commands
- my_turtle.fd(distance)
- my_turtle.left(degrees)
- my_turtle.pd()
- my_turtle.pu()