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
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Python November 14, Unit 7. Python Hello world, in class.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
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 Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
JavaScript, Fourth Edition
Basic Input/Output and Variables Ethan Cerami New York
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.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
Strings CS303E: Elements of Computers and Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Variables, Expressions and Statements
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
Chapter 2 Variables.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
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.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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++
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
BASIC ELEMENTS OF A COMPUTER PROGRAM
Multiple variables can be created in one declaration
Variables, Expressions, and IO
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
CS111 Computer Programming
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
Arithmetic Operations
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Chapter 2 Variables.
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 Assign the value 9 to the variable my_num Assign the value “17” to the variable my_string Print my_num+my_string What happens? Assign the value 17 to the variable my_string Print my_num+my_string What happens? Assign the value “print” to the variable print_var 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: 5+9/4*3-2 (5+9)/(4*(3-2)) 5**2+1/4-4 5**(2+1)/(4-5) 5**(2+1)/(4-4) ((4-2)/(3-8) ((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: print “\tName: Bob” print “\t Name:\n Bob” print “Name:\t Bob”

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