1 Computer Graphics and Games MONT 105S, Spring 2009 Lecture 2 Simple Python Programs Working with numbers and strings.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Python November 14, Unit 7. Python Hello world, in class.
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 7 Strings, Lists.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Munster Programming Training
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Microsoft Visual Basic 2005 BASICS Lesson 4 Mathematical Operators.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Chapter 2: Using Data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CS 102 Computers In Context (Multimedia)‏ 01 / 26 / 2009 Instructor: Michael Eckmann.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Maths in Python [ slide 5 ] 1.Copy the table 2.Race a friend with a calculator to see whether Python is faster than a calculator: a) 5 * 6.5 = b)7 / 3.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.
Chapter 2 Variables.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Python Let’s get started!.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
ECS 15 Strings, input. Outline  Strings, string operation  Converting numbers to strings and strings to numbers  Getting input  Running programs by.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Input, Output and Variables GCSE Computer Science – Python.
FIND THE VOLUME: 5 in 8 in 4 in.
Topics Designing a Program Input, Processing, and Output
Python Let’s get started!.
Data Types and Conversions, Input from the Keyboard
Data Types, Arithmetic Operations
Design & Technology Grade 7 Python
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Introduction to Python and programming
Examples of Primitive Values
Introduction to C++ Programming
More Maths Programming Guides.
Introduction In today’s lesson we will look at: why Python?
Topics Designing a Program Input, Processing, and Output
7 – Variables, Input and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Data Types and Expressions
Building Java Programs
Presentation transcript:

1 Computer Graphics and Games MONT 105S, Spring 2009 Lecture 2 Simple Python Programs Working with numbers and strings

2 Review Write a program that asks for a user's favorite food, and then says that it likes that food too. # Program: food.py # Purpose: Asks user for their favorite food, and tells # them it likes that food too. faveFood = raw_input("What is your favorite food? ") print "I love " + faveFood + " too!" What does this print out if the user enters 'Chocolate' when asked for their favorite food?

3 Classes and Objects Python is an object oriented programming language. All items of data are objects. Objects have properties: Things that describe the objects Objects have methods: Things an object can do. A class describes a group of objects that have the same methods and set of properties. Example: A car class Properties: color, number of doors, body type Methods: accelerate, brake, steer My car is an object that is a member of the car class.

4 Primitive Numeric Types TypeKind of infoUses integerwhole numberArithmetic expressions long integerLarge whole number " " floating point decimal number " " Examples: integer: long integer: L floating point:

5 Arithmetic Expressions with Floating Point Numbers Arithmetic operators for real numbers: +-*/** These work the way you would expect: * / **3.08.0

6 Arithmetic Expressions with Integers Arithmetic operators for integers: +-*/%** All work the way you would expect, except / and % DIV: Integer division truncates the decimal portion. 12 / 34 8 / 32 1 / 20 MOD: Gives the remainder of division of two integers: 7 % % 4 3

7 Example Using Numbers # Program: dogYears.py # Purpose: To calculate a dogs age in dogyears. ageOfDog = input("How old is the dog? ") dogYears = 7*ageOfDog print "The dog is", dogYears, "years old in dogyears."

8 Using commas vs. + in a print statement A + sign is used to concatenate (join) two strings. It can be used in a print statement to print two strings joined together. It cannot be used to join a string with a number. Example: myCat = "Calico" print "My cat is a " + myCat A comma is used to list items to print. It is used when we have several items of different types (e.g. a string and an integer) that we want to print. Example: myAge = 39 print "My age is", myAge

9 Strings A line of text is called a string. A string is indicated using quotes. Example: >>>"dog" 'dog' Triple quotes allow newlines in the middle of a string. Example: >>> ' ' 'This string has a break in the middle. ' ' ' 'This string has a break\nin the middle.'

10 String Methods Python provides a variety of operations with strings: Concatenation: +"dog" + "house" -> "doghouse" Repetition: *"cool"*3-> "coolcoolcool" Length: len(stringName)len("cool")-> 4 There are numerous other operations that can be applied to strings. Some of these are described in your textbook.