CSC 221: Introduction to Programming Fall 2018

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CSC 221: Computer Programming I Fall 2011
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
Python Programming Fundamentals
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Chapter 2: Using Data.
1 CSC 221: Introduction to Programming Fall 2012 Python data, assignments & turtles  Scratch programming review  Python & IDLE  numbers & expressions.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Fundamentals of Programming I Overview of Programming
Chapter 1.2 Introduction to C++ Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Chapter 1.2 Introduction to C++ Programming
Python Let’s get started!.
Introduction to Python
Introduction to Python and programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Chapter 3: Understanding C# Language Fundamentals
IDENTIFIERS CSC 111.
Introduction to Python and programming
Introduction to Python and programming
Number and String Operations
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Variables and Expressions
Reading Input from the Keyboard
Introduction to Python and programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 3 Mathematical Functions, Strings, and Objects
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Fundamental Programming
Topics Designing a Program Input, Processing, and Output
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
CS 100: Roadmap to Computing
Introduction to Python and programming
Class code for pythonroom.com cchsp2cs
Problem 1 Given n, calculate 2n
Data Types and Expressions
Chapter 5 JavaScript Numbers and Expressions
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

CSC 221: Introduction to Programming Fall 2018 Python data, assignments & functions data types numbers, strings, booleans functions & type conversions variables & assignments input & output modules turtle graphics, math, random Python documentation, help

Python number types Python distinguishes between different types of numbers int integer values, e.g., 2, -10, 1024, 99999999999999 can be specified in binary, octal or hexidecimal using '0b', '0o', and '0x' prefixes 0b1011  10112  1110 0o23  238  1910 0x1A  1A16  2610 float floating point (real) values, e.g., 3.14, -2.0, 1.9999999 scientific notation can be used to make very small/large values clearer 1.234e5  1.234 x 105  123400 9e-5  9 x 10-5  0.00009 complex complex numbers (WE WILL IGNORE)

Numbers & expressions standard numeric operators are provided recall in Scratch: standard numeric operators are provided + addition 2+3  5 2+3.5  5.5 – subtraction 10–2  8 99–99.5  -0.5 * multiplication 2*10  20 2*0.5  1.0 / division 10/2.5  4.0 10/3  3.333… ** exponent 2**10  1024 9**0.5  3.0 less common but sometimes useful % remainder 10%3  1 10.5%2  0.5 // integer division 10//4  2 10//4.0  2.0 note: spacing around an operator is optional: 2/3 = 2 / 3

Exercise: calculations use IDLE to determine the number of seconds in a day the number of seconds in a year (assume 365 days in a year) your age in seconds the number of inches in a mile the distance to the sun in inches the solution to the Wheat & Chessboard problem

Python strings in addition to numbers, Python provides a type for representing text a string is a sequence of characters enclosed in quotes Python strings can be written using either single- or double-quote symbols (but they must match) OK: "Creighton" 'Billy Bluejay' "Bob's your uncle" not OK: "Creighton' 'Bob's your uncle' can nest different quote symbols or use the escape '\' character "Bob's your uncle" 'Bob\'s your uncle'

Python strings (cont.) on rare occasions when you want a string to span multiple lines, use three quotes """This is a single string""" '''So is this''' the + operator, when applied to strings, yields concatenation "Billy " + "Bluejay"  "Billy Bluejay"

Boolean type Scratch had implicit true/false values recall that control blocks such as if, if-else, repeat-until had true/false tests that determined the action Python has explicit True and False values these values can be used in expressions, assigned to variables, used to control other statements, … True and False are Boolean values (named after mathematician George Boole)

type function in mathematics, a function is a mapping from some number of inputs to a single output e.g., square root function 9  3 e.g., maximum function max(3.8, 4.2)  4.2 Python provides numerous built-in functions for performing useful tasks the type function takes one input, a value, and returns its type as output

type conversions there are functions associated with each type for converting values str(X)  converts X into a string (with quotes around it) int(X)  converts X into an integer, if possible float(X) converts X into a real number, if possible bool(X)  converts X into a Boolean value (false if 0 or empty string, else true)

Variables & assignments recall from Scratch, variables were used to store values that could be accessed & updated e.g., the lines spoken by the sprites in conversation e.g., the number of times an event occurred similarly, in Python can create a variable and assign it a value the variable name must start with a letter, consist of letters, digits & underscores (note: no spaces allowed) an assignment statement uses '=' to assign a value to a variable general form: VARIABLE = VALUE_OR_EXPRESSION age = 20 seconds_in_day = 24 * 60 * 60 seconds_in_year = 365 * seconds_in_day name = "Prudence" greeting = "Dear " + name

Variable names some reserved words cannot be used for variable names Python libraries tend to use underscores for multi-word variable names first_name number_of_sides feet_to_meters note: capitalization matters, so first_name ≠ first_Name

Exercise: population growth visit http://www.census.gov/popclock for a current U.S. population estimate e.g., 328,553,944 at 3:15 pm (CDT) on 9/10/18 according to U.S. Census Bureau: one birth every 8 seconds one death every 12 seconds one naturalized citizen (net) every 28 seconds we want to predict the population some number of years in the future e.g., what would be the population 10 years from now? (assume 365 day years) \ is the line continuation character – can break a statement across lines

Issues (pt. 1) not readable significant duplication difficult to edit/fix/change we can address readability & duplication by introducing variables

Issues (pt. 2) variables make the code more readable and reduce duplication, but entering statements directly into the interpreter has limitations no way to go back and fix errors no way to save the code for later use better to enter the statements in a separate, editable, re-executable file the IDLE environment has a built-in file editor under the File menu, select New Window this will open a simple file editor enter the statements save the file (using FileSave) then load the function (using RunRun Module) the statements are loaded and run just as if you typed them in directly

Modules a module is a file of code that can be repeatedly edited and loaded/run lines starting with # are comments, ignored by the Python interpreter should start each file with a comment block documenting the file note: after running this module, you still need to query the interpreter for the value of new_population

Built-in print function the print function can automatically display a value or sequence of values (separated by commas) print(VALUE_1, VALUE_2, …, VALUE_n) now, a complete message containing the answer is displayed when the module is run

User input as is, changing the years_in_future assignment and performing a different calculation is easy but it would be even better to just ask for the number the built-in input function displays a prompt and reads in the user's input response = input("PROMPT MESSAGE") the input function always returns what the user typed as a string if you want to treat the input as a number, you must explicitly convert them int_response = int(input("PROMPT MESSAGE")) float_response = float(input("PROMPT MESSAGE"))

Final version now, can enter a different number of years each time the module is run

Exercise: reading level in the 1970's, the U.S. military developed a formula for measuring the readability of text (*for young adults) the FORCAST formula assigns a reading age to text based on the following: reading age = 20 – 15 × # single syllable words total # of words create a Python module that prompts the user for the # of single syllable words and total # of words in a text calculates the reading age for that text prints that reading age (in a readable format)

Built-in functions Python provides many built-in functions mathematically, a function is a mapping from some number of inputs to an output e.g., the abs function maps a single number to its absolute value e.g., the max/min functions map two or more numbers to the maximum/minimum number note that these functions do not just print the output, they return it that is, a function call can appear in an expression, its value is the output value

Modules & functions in addition to these basic, built-in functions, the math module contains other useful functions you load a module using import can then call functions from the math module as math.FUNC(…) also have constants for π and e

Why the prefix? the round function does not have a prefix: round(3.2) but the ceiling function does: math.ceil(3.2) when a module is loaded using import, the module prefix must be used to clearly identify functions (since different modules might have functions with the same name) no ambiguity for built-in functions or modules run directly in IDLE, so no prefix required

random module the random module contains functions for generating random values random() returns a float from range [0, 1) randint(X, Y) returns an int from range [X, Y] choice() returns a random character/item from a string/list

Turtle graphics module IDLE comes with a built-in module for turtle graphics must import the turtle module to make the code accessible can then call functions from the module via the notation turtle.FUNCTION() play with the executable code in the interactive book

Turtle commands in addition to movement commands (left, right, forward, backward), there are commands for setting color and pen width complete the exercise (ch3_2) – prompt the user for colors, pen size

For loops suppose we want to draw a square – need to draw a side & turn (4 times) a for loop specifies a repetitive task for VARIABLE in LIST: STATEMENTS the range function makes this even cleaner for VARIABLE in range(SIZE): STATEMENTS if you don't ever need the loop variable, use _ try the Turtle race lab (bottom of 4.8)

Python documentation you can get the latest Python details in IDLE by selecting Help>Python Docs Language Reference gives an overview of the language Library Reference documents the common library modules (e.g., math, random, turtle) Global Module Index allows you to search for modules by name

Python help modules are also self-documenting call the help function to see info on a function can also call help on the entire module