CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Introduction to C Programming
ITEC113 Algorithms and Programming Techniques
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2: Input, Processing, and Output
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to C Programming
INTRODUCTION TO PYTHON PART 2 INPUT AND OUTPUT CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
1 Introduction to Programming with Python. 2 Some influential ones: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Introduction to Python
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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
Computer Science 101 Introduction to Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Variables, Expressions and Statements
CS 100 Introduction to Computing Seminar September 21, 2015.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Python Let’s get started!.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
1 Introduction to Programming with Python: overview.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ First Steps.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Input and Output Upsorn Praphamontripong CS 1110
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2: Input, Processing, and Output
The Selection Structure
Variables, Expressions, and IO
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Reading Input from the Keyboard
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Input, Processing, and Output
Introduction to Programming with Python
Introduction to C Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University of Washington

Designing a Program Programs must be designed before they are written Program development cycle: Design the program Write the code Correct syntax errors Test the program Correct logic errors

Designing a Program (cont’d.) Design is the most important part of the program development cycle Understand the task that the program is to perform Work with customer to get a sense what the program is supposed to do Ask questions about program details Create one or more software requirements

Designing a Program (cont’d.) Determine the steps that must be taken to perform the task Break down required task into a series of steps Create an algorithm, listing logical steps that must be taken Algorithm: set of well-defined logical steps that must be taken to perform a task

Pseudocode Pseudocode: fake code Informal language that has no syntax rule Not meant to be compiled or executed Used to create model program No need to worry about syntax errors, can focus on program’s design Can be translated directly into actual code in any programming language

Flowcharts Flowchart: diagram that graphically depicts the steps in a program Ovals are terminal symbols Parallelograms are input and output symbols Rectangles are processing symbols Symbols are connected by arrows that represent the flow of the program

Input, Processing, and Output Typically, computer performs three- step process Receive input Input: any data that the program receives while it is running Perform some process on the input Example: mathematical calculation Produce output

Displaying Output with the print Function Function: piece of prewritten code that performs an operation print function: displays output on the screen Argument: data given to a function Example: data that is printed to screen Statements in a program execute in the order that they appear From top to bottom

Strings and String Literals String: sequence of characters that is used as data String literal: string that appears in actual code of a program Must be enclosed in single (‘) or double (“) quote marks String literal can be enclosed in triple quotes (''' or """ ) Enclosed string can contain both single and double quotes and can have multiple lines

Comments Comments: notes of explanation within a program Ignored by Python interpreter Intended for a person reading the program’s code Begin with a # character End-line comment: appears at the end of a line of code Typically explains the purpose of that line

Variables Variable: name that represents a value stored in the computer memory Used to access and manipulate data stored in memory A variable references the value it represents Assignment statement: used to create a variable and make it reference data General format is variable = expression Example: age = 29 Assignment operator: the equal sign (=)

Variables (cont’d.) In assignment statement, variable receiving value must be on left side A variable can be passed as an argument to a function Variable name should not be enclosed in quote marks You can only use a variable if a value is assigned to it

Variable Naming Rules Rules for naming variables in Python: Variable name cannot be a Python key word Variable name cannot contain spaces First character must be a letter or an underscore After first character may use letters, digits, or underscores Variable names are case sensitive Variable name should reflect its use

Displaying Multiple Items with the print Function Python allows one to display multiple items with a single call to print Items are separated by commas when passed as arguments Arguments displayed in the order they are passed to the function Items are automatically separated by a space when displayed on screen

Reading Input from the Keyboard Most programs need to read input from the user Built-in input function reads input from keyboard Returns the data as a string Format: variable = input(prompt) prompt is typically a string instructing user to enter a value Does not automatically display a space after the prompt

Reading Numbers with the input Function input function always returns a string Built-in functions convert between data types int(item) converts item to an int float(item) converts item to a float Nested function call: general format: function1(function2(argument)) value returned by function2 is passed to function1 Type conversion only works if item is valid numeric value, otherwise, throws exception

Performing Calculations Math expression: performs calculation and gives a value Math operator: tool for performing calculation Operands: values surrounding operator Variables can be used as operands Resulting value typically assigned to variable Two types of division: / operator performs floating point division // operator performs integer division Positive results truncated, negative rounded away from zero

CODING DETAILS

20 Variables variable: A named piece of memory that can store a value. Usage: Compute an expression's result, store that result into a variable, and use that variable later in the program. assignment statement: Stores a value into a variable. Syntax: name = value Examples: x = 5 gpa = 3.14 x 5 gpa 3.14 A variable that has been given a value can be used in expressions. x + 4 is 9

21 string: A sequence of text characters in a program. Strings start and end with quotation mark " or apostrophe ' characters. Examples: "hello" "This is a string" "This, too, is a string. It can be very long!" A string may not span across multiple lines or contain a " character. "This is not a legal String." "This is not a "legal" String either." A string can represent characters by preceding them with a backslash. \t tab character \n new line character \" quotation mark character \\ backslash character Example: "Hello\tthere\nHow are you?" Strings

22 String properties len( string ) - number of characters in a string (including spaces) str.lower( string ) - lowercase version of a string str.upper( string ) - uppercase version of a string Example: name = "Martin Douglas Stepp" length = len(name) big_name = str.upper(name) print(big_name, "has", length, "characters“) Output: MARTIN DOUGLAS STEPP has 20 characters

23 print : Produces text output on the console. Syntax: print " Message " print Expression Prints the given text message or expression value on the console, and moves the cursor down to the next line. print Item1, Item2,..., ItemN Prints several messages and/or expressions on the same line. Examples: print("Hello, world!") age = 45 print("You have", 65 - age, "years until retirement") Output: Hello, world! You have 20 years until retirement print

24 input : Reads a number from user input. You can assign (store) the result of input into a variable. Example: age = input("How old are you? ") print("Your age is", age) print("You have", 65 - age, "years until retirement“) Output: How old are you? 53 Your age is 53 You have 12 years until retirement input

25 if if statement: Executes a group of statements only if a certain condition is true. Otherwise, the statements are skipped. Syntax: if condition : statements Example: gpa = 3.4 if gpa > 2.0: print("Your application is accepted.“)

26 if/else if/else statement: Executes one block of statements if a certain condition is True, and a second block of statements if it is False. Syntax: if condition : statements else: statements Example: gpa = 1.4 if gpa > 2.0: print("Welcome to Mars University!“) else: print("Your application is denied.“) Multiple conditions can be chained with elif ("else if"): if condition : statements elif condition : statements else: statements