Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
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.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Python November 14, Unit 7. Python Hello world, in class.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Basic Input/Output and Variables Ethan Cerami New York
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
A First Book of ANSI C Fourth Edition
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Variables, Expressions and Statements
Chapter 2 Variables.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Course A201: Introduction to Programming 09/09/2010.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Chapter 1: Introduction to computers and C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variables, Expressions, and IO
Introduction to C++ Programming
CS111 Computer Programming
Chapter 2 Variables.
“If you can’t write it down in English, you can’t code it.”
Reading Input from the Keyboard
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Variables, Data Types & Math
CS150 Introduction to Computer Science 1
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Chapter 2 Variables.
Introduction to C Programming
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Variables and I/O

Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?

Variables and Assignment A name that refers to a value Python uses dynamic typing my_num = 6 my_string = “Hello” another_num = my_num

Variables and Assignment = often read as “gets the value” my_num and another_num refer to the same object my_num = 6 my_string = “Hello” another_num = my_num 6 “Hello” my_string my_num another_num

Variables and Assignment Numbers and strings are immutable my_num = 6 my_string = “Hello” another_num = my_num my_num = 7 my_num = “CS” “Hello” my_string my_num another_num 7 6

Variable Names A combination of letters, digits, and _ Must begin with a letter Case sensitive OKAY –csiscool, my_variable variable2 NOT OKAY –cs is cool, 2ndvariable, print Why not print?

Exercises 1.Assign the value 9 to the variable my_num 2.Assign the value “17” to the variable my_string 3.Print my_num+my_string 4.What happens? 5.Assign the value 17 to the variable my_string 6.Print my_num+my_string 7.What happens? 8.Assign the value “print” to the variable print_var 9.What happens?

Operators You’ve seen + -, *, /, ** (exponentiation) % - remainder –12%6 –12%5 What is the result of 5/2?

Operators What is the result of 5/2? 2 Why? –if both operands are integers, integer division is performed and the result must be an integer –result is truncated

Precedence PEMDAS –parentheses –exponents –multiplication –division –addition –subtraction Evaluation done left to right

Alternatives +=, -=, *=, /= num += 3 -> num = num + 3

Exercises 1.Determine the results of the following: 1.5+9/4*3-2 2.(5+9)/(4*(3-2)) 3.5**2+1/ **(2+1)/(4-5) 5.5**(2+1)/(4-4) 6.((4-2)/(3-8) 7.((5+3)/3(2+1))

Strings Concatenation –print “Hello, “ + “World!” –print “Hello “ + “Class!” –print “Hello” + “Class!” Repetition –print “Hello” * 3 –print “Hello,” * 3

Strings Can be in single or double quotes –“hello” or ‘hello’ Escape sequences encode special characters –\n = newline, \t = tab, \\ = \, \” = “, \’ = ‘ –can also use “ in string enclosed by ‘’ and ‘ in string enclosed by “” “it’s fun”, ‘a “sample” string’ ‘it\’s fun’, “a \”sample\” string” –lists python escape sequences

Exercises 1.Execute the following statements: 1.print “\tName: Bob” 2.print “\t Name:\n Bob” 3.print “Name:\a Bob” 4.print “\a”*10

Composition What is the result of the following: age = 19 print “Your age is “ + age Instead, use ‘,’ to compose statements age = 19 print “Your age is “, age

Keyboard Input input( ) reads an integer/float from the keyboard raw_input( ) reads a string from the keyboard Syntax –variable_name = input( ) –variable_name = raw_input( ) Examples –mynum = input(“Enter number: “) –mystring = raw_input(“Enter string: “)

Keyboard Input Examples mynum = input(“Enter number: “) same as print “Enter number: “ mynum = input() Recall, an int can be a string, but a string cannot be an int

Exercises 1.Write the algorithm for a program that prompts the user for two integers and displays the sum, difference, product, and quotient of the numbers. 2.Write a program that implements the algorithm you wrote for exercise 1.

Exercises 3.Write the algorithm for a program that stores your name, age, street number, street name, city, state, and zip code in separate variables and the displays the data in the following format: My name is : Mickey Mouse My age is: 75 My address is: 1234 Main Street, San Francisco, CA Write a program that implements the algorithm you wrote for exercise 3