Variables, Constants, and Data Types

Similar documents
CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter. Let's explore some other fundamental programming concepts

A variable is a name for a location in memory A variable must be declared

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Chapter 2: Data and Expressions

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

Java Basic Datatypees

Chapter 2: Data and Expressions

2 rd class Department of Programming. OOP with Java Programming

Chapter 2: Data and Expressions

Basics of Java Programming

Program Elements -- Introduction

Chapter 2 Working with Data Types and Operators

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

Full file at

CS313D: ADVANCED PROGRAMMING LANGUAGE

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Visual C# Instructor s Manual Table of Contents

Getting started with Java

CMPT 125: Lecture 3 Data and Expressions

Chapter 2: Using Data

PYTHON- AN INNOVATION

The Warhol Language Reference Manual

Chapter 2. Lexical Elements & Operators

4. Inputting data or messages to a function is called passing data to the function.

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Full file at

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Lecture 2 Tao Wang 1

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

2: Basics of Java Programming

Chapter 2 Elementary Programming

Declaration and Memory

COMP Primitive and Class Types. Yi Hong May 14, 2015

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 6 Primitive types

Advanced Algorithms and Computational Models (module A)

CS11 Java. Fall Lecture 1

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

B.V. Patel Institute of BMC & IT, UTU 2014

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

CS112 Lecture: Primitive Types, Operators, Strings

6.096 Introduction to C++ January (IAP) 2009

Chapter 2 Basic Elements of C++

LECTURE 02 INTRODUCTION TO C++

Programming in Python 3

Number Systems, Scalar Types, and Input and Output

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Language Reference Manual

4 Programming Fundamentals. Introduction to Programming 1 1

Strings, characters and character literals

Chapter 2: Using Data

Bits. Binary Digits. 0 or 1

CSc Introduction to Computing

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Python Input, output and variables

Variables and Values

COMP6700/2140 Data and Types

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Introduction to Python Programming

FRAC: Language Reference Manual

PHP. Interactive Web Systems

Programming for Engineers Introduction to C

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Java Bytecode (binary file)

L-System Fractal Generator: Language Reference Manual

Lecture Set 2: Starting Java

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

ECE 122 Engineering Problem Solving with Java

Lecture Set 2: Starting Java

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Final Labs and Tutors

BITG 1233: Introduction to C++

Java Notes. 10th ICSE. Saravanan Ganesh

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CPS122 Lecture: From Python to Java

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Operations. Making Things Happen

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CP122 Computer Science I. Chapter 2: Data Types, Variables, and I/O

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

What did we talk about last time? Examples switch statements

The C++ Language. Arizona State University 1

2 nd Week Lecture Notes

A First Program - Greeting.cpp

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Lexical Considerations

CS 115 Lecture 4. More Python; testing software. Neil Moore

Transcription:

Variables, Constants, and Data Types Strings and Escape Characters Primitive Data Types Variables, Initialization, and Assignment Constants Reading for this lecture: Dawson, Chapter 2 http://introcs.cs.princeton.edu/python/12types

Character Strings So far, all of our program data has been text in string form. A string is, quite literally, a string of characters Test can be represented as a string literal by bounding it with a pair of double quotes OR a pair of single quotes. (Must match!) Examples: "This is a string literal." 'X' '123 Main Street' "" (empty string) The word "literal" indicates that we are directly coding the information rather that getting it indirectly.

Combining Strings To combine (or "concatenate") two strings, we can use the plus sign "Peanut butter " + "and jelly" You may find this helpful when printing output where some parts of the text may vary while other parts remain the same. Consider this example: name = "Bob" print ("Hello, " + name + "...welcome!") Prints: Hello, Bob...welcome!

String Concatenation The + operator is also used for arithmetic addition The function that it performs depends on the type of the information on which it operates If both operands are strings, it performs string concatenation If both operands are numeric, it adds them "Hello " + "world" gives you "Hello world" 4 + 42 gives you 46 NOTE: You cannot directly concatenate a string and a number:

String Concatenation However, it will work if you first convert the number to its string equivalent: print ("My favorite number is " + str(7)) My favorite number is 7 This has to do with the behavior of different data types in Python. Other programming languages create different restrictions and allowances based on how their data types are set up

Escape Sequences What if we want to include the quote character itself? The following line would confuse the interpreter because it would interpret the two pairs of quotes as two strings and the text between the strings as a syntax error: print ("I said "Hello" to you.") A String Syntax Error A String One option would be to replace the beginning and ending double-quote symbols with single-quotes: print ('I said "Hello" to you.') The reverse would also be valid print ("I said 'Hello' to you.")

Escape Sequences Another option is to use escape sequences, which are character combinations that have a special meaning within a string Some Escape Sequences: Escape Sequence Meaning Example: print ("Hello,\n\tworld") Hello, world \t \n \r \" \' \\ tab newline carriage return double quote single quote backslash

Useful string methods Using a string method requires three things: 1) A reference to the string, such as a string literal or a variable 2) The method name, such as upper 3) The argument list, a pair of parentheses () with a list of values inside. May be empty Example: print ("Hello") Hello print ("Hello".upper()) HELLO print ("Hello".lower()) hello See Table 2.3 on page 38 of the textbook for more methods you can use

Number Bases You are probably used to numbers in base-10, where each digit is a 0-9 (10 possible values) The base specifies how many values can be expressed using a particular number of digits. For example, 3 base-10 digits can express 1000 different values: 000-999 In other words, the base raised to the power the number of digits Example:10^3 = 1000

Number Bases In addition to base-10, you will also see other types, such as the following: Binary: base-2, every digit is a 0 or 1 Octal: base-8, every digit is a 0-7 Hexadecimal: base-16, every digit is a 0-15; digits 10-15 become a-f I recommend researching this topic to become more familiar In programming, you will encounter binary very frequently because that is how data is stored

Number Bases Binary You are probably familiar with "bytes" as a unit of computer storage A byte is made of 8 bits, where each bit is a 0 or 1 in other words, binary You have progressively larger forms of storage: bits bytes kilobytes megabytes gigabytes TERABYTES!!!

Types of Data In Python, all data are objects You will work mainly with two types of data: Built-in data types: These include most basic forms of data you will see in your programs Complex data types (my wording): Conglomerations of other data types, both built-in and other complex types We will introduce types as needed

Some Primitive Types We call these "primitive" because they form the basis for other more complex data types Three numeric types: int float complex True/False (or "boolean") values: bool A type for text (i.e., strings): str

Numeric Primitive Data The int type is for whole numbers: 7, -358, 0, -10, 12398 The float type is for decimal (or "floating-point") numbers: 7.6, -35.8, 0.0, -1.09, 12398.0 The complex type is for numbers with an imaginary component. (We probably will not use this type.) Each of these will have different behaviors and limitations, depending on a number of factors

Boolean Primitive Data A bool type can have either of two values: True False True and False are reserved words in Python A bool type can be useful for representing any two states such as a light bulb being on or off on = True

String (str) Data As mentioned earlier, a "string" is a sequence of zero or more characters You will use strings often, in different ways: Printing as output Fetching as input Comparing Reversing Converting to/from other types Work and practice to become comfortable with this type and its many uses

Characters Some languages, such as Java, have a character type, specifically Python does not, though, and if you need to use a character, you will likely just use a string consisting of a single character Each character, however, will correspond to an integer value in some character set, and there are methods to perform conversions: Integer to character: chr Example: chr(97) a Character to integer: ord Example: ord('a') 97

Character Sets A character set is an ordered list of characters, with each character corresponding to a unique number Python uses the Unicode character set The Unicode character set uses sixteen bits per character, allowing for 65,536 (2^16) unique characters It is an international character set, containing symbols and characters from many world languages

Characters The ASCII character set is older and smaller (8-bit) than Unicode, but is still quite popular (in C programs) The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, a,,, period, semi- olon, 0,,, &,, \, carriage return, tab,...

Variable Declaration A variable is a name for a location in memory A variable must be declared by specifying its name and its initial value name = "Bob". body_temp = 98.6. light_on = False. In some languages (e.g., Java), variables are of a specific type, but Python is more flexible

Constants A constant is an identifier that is similar to a variable except that it is meant to hold the same value during its entire existence As the name implies, it is constant, not variable In Python, we indicate a constant using ALL CAPS MIN_HEIGHT = 69. This indicates that the value should not be changed after it is first declared Some programming languages will actually forbit you to change the value of a constant

Constants Constants are useful for three important reasons First, they give meaning to otherwise unclear literal values For example, NUM_STATES is more meaningful than the literal 50 Second, they facilitate program maintenance If a constant is used in multiple places and you need to change its value later, its value needs to be updated in only one place what if the country gets a 51 st state? Rather than having to find and change it in multiple places! Third, they formally show that a value should not change, avoiding inadvertent errors by other programmers

Value Assignment An assignment statement gives the variable an actual value in memory The equals sign provides this function total = 55 The expression on the right is evaluated and the result is stored as the value of the variable on the left Any value previously stored in total is overwritten Unlike some other languages, Python allows you to store any type of data in any variable. Other languages - like Java will restricted the kinds of values you can assign to a variable, based on its type

Variables and Literals i = 7 j = -8.7 k = 9 c = Hello World" is_it_on = True