Variables and Values

Similar documents
cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

CMPT 125: Lecture 3 Data and Expressions

Java Notes. 10th ICSE. Saravanan Ganesh

Basics of Java Programming

Reserved Words and Identifiers

Lexical Considerations

Programming for Engineers Introduction to C

Full file at

Chapter 2 Working with Data Types and Operators

PYTHON- AN INNOVATION

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Work relative to other classes

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

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

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Java enum, casts, and others (Select portions of Chapters 4 & 5)

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

4 Programming Fundamentals. Introduction to Programming 1 1

Lexical Considerations

Variables, Constants, and Data Types

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Visual C# Instructor s Manual Table of Contents

Variables and literals

1 Lexical Considerations

UNIT- 3 Introduction to C++


Introduction to Python Programming

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

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

Chapter 2. Lexical Elements & Operators

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Chapter 17. Fundamental Concepts Expressed in JavaScript

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Full file at

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Chapter 6 Primitive types

CGS 3066: Spring 2015 JavaScript Reference

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

ENGR 101 Engineering Design Workshop

Typescript on LLVM Language Reference Manual

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Our Strategy for Learning Fortran 90

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

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

The type of all data used in a C++ program must be specified

corgi Language Reference Manual COMS W4115

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

2 nd Week Lecture Notes

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Chapter 2 Elementary Programming

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

Maciej Sobieraj. Lecture 1

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Chapter 2: Data and Expressions

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Unit 3. Constants and Expressions

JME Language Reference Manual

Chapter 2 Basic Elements of C++

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

Digital Computers and Machine Representation of Data

Data Representation 1

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

3 The Building Blocks: Data Types, Literals, and Variables

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Announcement. (CSC-3501) Lecture 3 (22 Jan 2008) Today, 1 st homework will be uploaded at our class website. Seung-Jong Park (Jay)

Lecture 2 Tao Wang 1

Getting started with Java

The C++ Language. Arizona State University 1

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

L-System Fractal Generator: Language Reference Manual

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

MACHINE LEVEL REPRESENTATION OF DATA

CS-201 Introduction to Programming with Java

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

Operators. Java operators are classified into three categories:

IT 374 C# and Applications/ IT695 C# Data Structures

JAVA Programming Fundamentals

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

FRAC: Language Reference Manual

Java Programming Fundamentals. Visit for more.

Types, Operators and Expressions

CS112 Lecture: Primitive Types, Operators, Strings

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Transcription:

Variables and Values

Names Variables (which hold values) and functions (which are blocks of code) both have names Names must begin with a letter and may contain letters, digits, and underscores Names are case-sensitive total, TOTAL, and Total are three different names There are a number of reserved words, such as if and while, that cannot be used as the name of a variable or function 2

Style of names Style is all the little things, not required by the language, that make a program easier or harder to read For example: Lines of code should not extend past about column 80, because long lines are harder to read (especially if you have to scroll sideways!) Some style rules are just commonly accepted conventions, as in, This is the way we do things Style rule: Variable names should always begin with a lowercase letter In Java, names composed of multiple words are (almost) always written in camelcase, for example, sumofangles In Python, camel case is sometimes seen, but much more often, multiword name use underscores, for example, sum_of_angles Style rule: In this course, use underscores for Python, camelcase for Java 3

Importance of style Programs are read more often than they are written Estimates range from 20 times to 50 times The easier a program is to read, the easier it is to: Understand Debug Enhance Modify/update Style is less important for very small programs (say, less than 1000 lines) Without good style, even moderately sized programs become difficult or impossible to debug, let alone enhance or update This course is not about writing very small programs! 4

Strings Strings are composed of zero or more characters Like everything else on the computer, characters are represented in binary (a sequence of zeros and ones) Until recently, ASCII (American Standard Code for Information Interchange) was the most commonly used encoding ASCII allowed for 127 characters; for example, the letter a was represented by 01100001 ASCII was fine for representing English text, digits, and a handful of punctuation marks Unicode is an extension of ASCII that allows for hundreds of thousands of characters Python 2 uses ASCII; Python 3 uses Unicode 5

Strings may be enclosed in; Writing strings Single quotes, 'Like this' Double quotes, "Like this" So-called triple quotes, '''Like this''' or """Like this""" You can put double quotes inside a single-quoted string, or single quotes inside a double-quoted string, or either inside a triple-quoted string You can put a single quote inside a single-quoted string if you escape it, like this: \' The same goes for double quotes inside doubly-quoted strings: \" Example: "She said, \"Don't\"" Triply-quoted strings can extend across several lines; other kinds cannot 6

Additional escaped characters Some single characters cannot easily be entered directly into strings, and must be escaped (backslashed) \n represents a newline character \t represents a tab character \' represents a single quote (inside a singly-quoted string) \" represents a double quote (inside a doubly-quoted string) The above do not work inside triply-quoted strings Characters not in ASCII, but just in Unicode, are written as \uhhhh, where the hs are hexadecimal digits (0 1 2 3 4 5 6 7 8 9 A B C D E F) Example: \u03c0 is π Unicode characters do work in triply-quoted strings You can look up the character codes on the web 7

Ways to write integers Integers can be written in binary (base 2), octal (base 8), decimal (base 10) or hexadecimal (base 16) By default, integers are decimal Binary integers are written with an initial 0b Octal integers are written with an initial 0o Hexadecimal integers are written with an initial 0x In a string, Unicode characters are written as \u followed by four hexadecimal digits Decimal numbers other than 0 may not be written with an initial 0 8

Ways to write floats There is seldom any reason to write floating-point numbers in a base other than decimal Any number with a decimal point is a floating-point number Examples: 12.5, 12.,.5 Any number in scientific notation is a floating-point number Avogadro s number in scientific notation is 6.022 10 23 Since ASCII had neither the symbol nor superscripts, we use E or e to indicate...times 10 to the... Hence Avogadro s number has to be written as 6.022E23 9

Arithmetic expressions Just as in algebra, operations have precedence The unary operators + and - are done first Next comes exponentiation, ** Next multiplication (*) and division, ( /, //, %) Finally addition + and subtraction - Parentheses, (), can be used to alter the order of operations Brackets, [], and braces, {}, cannot be used for this purpose If you learned a variant of English where, for example, () were called brackets, that is not how these terms are used in programming! 10

Style in expressions Good style: x = -b + sqrt(b ** 2-4 * a * c) Poor style: x=-b+sqrt(b**2-4*a*c) Just as in English, it shardertoreadan expression whenthere aren tspaceswheretheybelong Rule: Put spaces around all binary operators There is no space after a unary operator, such as -b in the above example, or between a function name and the opening parenthesis Rule: Do not put spaces immediately inside parentheses Your textbook puts spaces here, as for example print( "hello" ) but this is very unusual, and I strongly discourage doing so 11

Boolean expressions Boolean expressions use the literal values True and False, and the logical operators and, or, and not not, being unary, has the highest precedence and has higher precedence than or Example: p and q or not r means the same as (p and q) or (not r) Other operators all have higher priority, so not p == q means not (p == q) When in doubt, use parentheses! 12

Boolean style 1 In Python, as in some other languages, tests don t always have to be Booleans Zero and a few other things typically mean false, things not considered false mean true Example: if a - b: print("unequal") else: print("equal") will print equal if a == b This sort of thing is necessary in the C language, which doesn t have Booleans, but is unnecessary and undesirable in Python, which does have Booleans if a!= b is much clearer than if a - b (Remember,!= means not equal to ) Rule: Only use Booleans for test conditions. 13

Rule: Avoid double negatives. Boolean style 2 In an if statement, this means putting the positive case first Example: Don t do this: if a!= b: # What to do when they a and b are not equal else: # What to do when a and b are not not equal Possible exception: If the negative case is short and the positive case is very long, it may be better to put the shorter case first Rule: Never compare a Boolean result to True or False For example, suppose you have a function isprime(n) to test whether a number n is prime or not prime (the function returns True or False). Then You can say if isprime(n): You could say if isprime(n) == True:, but it s redundant and just looks silly 14

Bitwise operators It is sometimes convenient to work with a sequence of bits (0s and 1s) Here are examples of each of the bit operators: Not: ~0b1100 == 0b0011 And: 0b1100 & 0b1010 == 0b1000 Or: 0b1100 0b1010 == 0b1110 Exclusive or: 0b1100 ^ 0b1010 == 0b0110 Left shift: 0b00010011 << 2 == 0b01001100 Right shift: 0b01001100 >> 2 == 0b00010011 15

Assignment abbreviations = means assignment: The variable on the left gets the value of the expression on the right largestvalue = largestvalue + increment may be abbreviated to largestvalue += increment largestvalue = largestvalue - increment may be abbreviated to largestvalue -= increment...and similarly for all the other operators bitsequence = bitsequence & mask may be abbreviated to bitsequence &= mask Etc. 16

The End Give a person a program, and you frustrate them for a day; Teach a person to program, and you frustrate them for a lifetime. --Anonymous 17