CS 177 Week 4 Recitation Slides Variables, Files and Functions.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

CS1315: Introduction to Media Computation Introduction to Programming.
Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Introduction to C Programming
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2 Writing Simple Programs
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Computer Organization - Syscalls David Monismith Jan. 28, 2015 Based on notes from Patterson and Hennessy Text and from Dr. Bill Siever.
Munster Programming Training
Programming Training Main Points: - Python Statements - Problems with selections.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
INTRODUCTION TO PYTHON. 1. The 5 operators in Python are: + - * / %
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
ITBP 119 Algorithms and Problem Solving Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Variables and Expressions CMSC 201 Chang (rev )
CS1315: Introduction to Media Computation Introduction to Programming.
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Announcements Survey link (can be found on piazza): OCJ6
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Python Let’s get started!.
1 CS 177 Week 2 Recitation Slides Variables, Files and Functions.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
JavaScript Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
GCSE Computing: Programming GCSE Programming Remembering Python.
CS1315: Introduction to Media Computation Introduction to Programming.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Introduction to Python Lesson 2a Print and Types.
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.
Chapter 2 Writing Simple Programs
Multimedia Summer Camp
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
Python Let’s get started!.
Formatting Output.
Programming Fundamental
CS1315: Introduction to Media Computation
Variables, Expressions, and IO
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
“If you can’t write it down in English, you can’t code it.”
Introduction to Programming
Elements of a Python Program
COMPUTER PROGRAMMING SKILLS
Comparing Python and Java
Introduction to Programming
Class code for pythonroom.com cchsp2cs
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

CS 177 Week 4 Recitation Slides Variables, Files and Functions

Announcements

ANY QUESTIONS?

Table of content Variables Files Functions o What is a function? o Why to use a function?

Variables Are names for objects that have values in main memory. We define variables by assigning values to names: Example: >>>a = 9 >>>b = >>>c = “Computer” Variables can change their values during the program. It’s important to choose good names that describes the variable. Example: firstName, courseNo, etc.

Assignment Statement Example: X = 10 Variable is also called Left Hand Side. Value is also called Right Hand Side. Variable Value

Assignment Statement The value can be: a number, a string of characters, but also a variable, or an expression. Example (decimal integer numbers): X = 10 Y = X Y = X + 5 Z = Y Variable Value

Example 1 What is the output of the following statements: >>> X = 10 >>> Y= 20 >>> result = X+ Y >>> print result >>> Y= 100 >>> print x + 10 >>> print x >>> print y X 10 Y result output

Exercise 1 >>> name = “John” >>> price = 9.99 >>> total = price / 3.0 >>> name = “Jane” >>>print name >>>print price >>>print total 3.33 total 9.99price name Jane output John Jane

Exercise 2 >>> var1 = “Purdue” >>> var2 = “University” >>>print var1 >>>print var2 >>>var2 = var1 >>>print var1 >>>print var2 “Purdue” var1var2 Purdue University Purdue output “University” “Purdue”

What about … >>> X + 2 = 15 >>> Y = “Hello” + 10 Tips: Left Hand Side should always be a variable You can’t perform operations (such as sum) on two values of different types (numeric and string)

Files Are collection of larger objects and values (byte) on the hard disk of the computer. Files can contain text, picture, sound, video, etc. Files have names File names have suffixes (.jpg,.wav,.mp3, and so forth)

Functions Collection of instructions that perform a task as: o Printing your name and course. o Calculating the average of set of numbers. o Editing a picture or video.

Functions Like in algebra, a function is a kind of “box” into which you put one value and out comes another. We represent (“denote”) a function by a name (in math we use f or F). F output Input

Why to use a function? If we define a function to perform a task, then we will write it once then we can use it (or call it) many times.

How to write functions? def functionName(): statement #1 statement #2 …

def SayHello: print(“Hello world!”) print(“--From Python”) Note: 1. Never forget the colon(:) 2. Align the statements in one function

No input arguments, no variable returned def Greet(): print("Hello Jack") print("Hello Tom") def main(): Greet()

Multiple arguments, returns one result def Average(a, b): return (a+b)/2 def main(): ave = Average(3, 4)

Returns more than one variable def getabc(): a = "Hello" b = "World" c = "!" return a,b,c def main(): a, b, c = getabc()

def Sum(a, b, c): return (a+b+c) def Greet(name, GPA): print("Hello", name) print("You have a GPA of ", GPA) def Div(a, b): return a/b def Mul(a, b): return a*b main()

# assign values to variables G1 = 4 G2 = 3.7 G3 = 3.7 C1 = 3 C2 = 1 C3 = 2 name = "Tom“

GPA = Div(Sum(Mul(G1, C1), Mul(G2, C2), Mul(G3, C3)), Sum(C1, C2, C3)) Greet(name, GPA) input() The output of Mul() is input of Sum() The output of Sum() is the input of Div()

What can go wrong? If your parrot is dead, consider this: o Did you use the exact same names (case, spelling)? o All the lines in the block must be indented, and indented the same amount. o Variables in the command area don’t exist in your functions, and variables in your functions don’t exist in the command area. o The computer can’t read your mind.  It will only do exactly what you tell it to do.  In fact, programs always “work,” but maybe not how you intended!

Final QUESTIONS???