Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
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
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Introduction CSE 1310 – Introduction to Computers and Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to Python
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to Programming with RAPTOR
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variables, Expressions and Statements
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Variables, Types, Expressions Intro2CS – week 1b 1.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
A Sample Program #include using namespace std; int main(void) { cout
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to Python Lesson 2a Print and Types.
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.
Fundamentals of Programming I Overview of Programming
Topic: Python’s building blocks -> Variables, Values, and Types
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Chapter 2 - Introduction to C Programming
First Programs CSE 1310 – Introduction to Computers and Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Variables, Types, Operations on Numbers
Expressions and Assignment
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of this material has been created by Dr. Darin Brezeale and Dr. Gian Luca Mariottini 1

Expression An expression is a piece of code that evaluates to a value. – That value is called a return value. The concept of an expression is key in this course, we will refer to it many times. It is VERY important to differentiate what is an expression and what is not an expression. 2

Is it an Expression? 12 1+(2**3) a = 5+12 b c = raw_input("enter a number: ") raw_input("enter a number: ") 3

Is it an Expression? 12 yes 1+(2**3) yes a = 5+12 no b yes, if b is a defined variable c = raw_input("enter a number: ") no raw_input("enter a number: ") yes 4

Is it an Expression? 12 yes, return value 12 1+(2**3) yes, return value 9 a = 5+12 no b yes, if b is defined, returns value of b c = raw_input("enter a number: ") no raw_input("enter a number: ") yes, return value is a string of whatever we type. 5

How Can We Tell If Something is an Expression? "Easy" way: type it in on the Python shell, and see if we get back a result. – Useful for experimentation, quick checks. An example: a = 5 in some languages (e.g., C++) a = 5 is an expression whose result is 5. To figure out if it is an expression in Python, you can search on the documentation (potentially time-consuming) or type it in and see if you get a result. (The result is that a = 5 is NOT an expression). 6

How Can We Tell If Something is an Expression? "Thinking" way: understand what are expressions, learn some rules. – No need to memorize lots of rules, just identify some basic broadly applicable rules that cover many cases. Example rule #1: Arithmetic calculations, involving numbers and variables, are expressions. 7

How Can We Tell If Something is an Expression? "Thinking" way: understand what are expressions, learn some rules. – No need to memorize lots of rules, just identify some basic broadly applicable rules that cover many cases. Example rule #2: a piece of code that is legal to appear at the right side of the assignment operator (i.e., the = sign) is an expression. expression c = raw_input("enter a number: ") 8

How Can We Tell If Something is an Expression? NOTE: both the "easy" way and the "thinking" way lead to the same conclusions. The two rules shown before are worth memorizing (or keeping handy in your notes). This is a more general guideline. Oftentimes there are multiple ways to reason about an aspect of programming. However, all correct ways should lead to the same conclusion. 9

Trick Question Is this an expression? 12 minus 5 10

Trick Question Is this an expression? 12 minus Let's try answering this question the "easy" way, by typing it in: >>> 12 minus 5 11

Trick Question Is this an expression? 12 minus Let's try answering this question the "easy" way, by typing it in: >>> 12 minus 5 SyntaxError: invalid syntax 12

Trick Question Is this an expression? 12 minus Let's try answering this question the "easy" way, by typing it in: >>> 12 minus 5 SyntaxError: invalid syntax 12 minus 5 is not valid code (gives an error, not a result value), so it is not an expression. 13

Locating Expressions An expression can be an entire line of code. An expression can appear as part of a line of code. Simple expressions can be combined into complicated expressions. – Example: *9/3 Three subexpressions: 12, 5, 12*9/3, connected by +. 12*9/3 can be further decomposed… 14

Locating Expressions Can you find expressions that are entire lines? # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print "Circumference = ", circumference # compute and print the area area = (radius ** 2) * pi print "area = ", area 15

Locating Expressions Can you find expressions that are entire lines? NO # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print "Circumference = ", circumference # compute and print the area area = (radius ** 2) * pi print "area = ", area 16

Locating Expressions Can you find expressions that are parts of lines? # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print "Circumference = ", circumference # compute and print the area area = (radius ** 2) * pi print "area = ", area 17

Locating Expressions Can you find expressions that are parts of lines? YES # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print "Circumference = ", circumference # compute and print the area area = (radius ** 2) * pi print "area = ", area Highlighted in red you see some examples of expressions, there are more examples than the ones shown. Remember: -Simple expressions can be parts of more complicated expressions. - Expressions may consist of more simple expressions. 18

What Does Python Do With Expressions? Every time Python sees an expression, it evaluates it. This is how Python computes. When Python evaluates a longer piece of code that includes an expression: – Python computes the return value of the expression. – Python substitutes the return value for the expression itself. 19

An Example of Expression Evaluation >>> print 12+3 Here we have a line of code that includes expression When Python evaluates that line of code, it computes the return value of 12+3, which is 15. Then, Python simplifies the code that is evaluated, by substituting the return value for the expression. Thus, "print 12+3" becomes "print 15". 20

One Expression vs. Many Expressions Note: (kind of obvious, but I have seen many people make this mistake, which is serious): Two (or more) expressions are NOT an expression. print "some text here", some_variable – How many expressions is the word "print" followed by? 21

One Expression vs. Many Expressions Note: (kind of obvious, but I have seen many people make this mistake, which is serious): Two (or more) expressions are NOT an expression. print "some text here", some_variable – print is followed by TWO expressions separated by a comma. These two expressions do NOT make up a single expression. – The comma is NOT part of either expression. – Yes, understanding code means understanding such details. 22

One Expression vs. Many Expressions Note: (kind of obvious, but I have seen many people make this mistake, which is serious): Two (or more) expressions are NOT an expression. my_variable = – Here, is a single expression, containing two more simple expressions (41, and 31) – The + operator is a part of the single expression It is NOT a part of the two simple expressions 41, 31. The + operator is used to COMBINE those two simple expressions into a new, more complicated expression. – There is no theoretical limit (just memory limits of the computer) on how large/complicated an expression can be. 23

Checklist You should now be able to: – Define what an expression is. – Be able to determine if any piece of code is an expression or not. – Be able to identify expressions in any line of code. – Be able to identify whether an expression contains more simple expressions. – Be able to write some expressions by yourself (TRY IT). 24

Statements Consider this line of code: print "Circumference = ", circumference This line is NOT an expression (although it contains expressions). This line of code "does something", namely it prints out some information. Statements are pieces of code that are NOT expressions, but do something. 25

Statements and Side Effects What a statement does is called a side effect. Examples of side effects: – Printing out a message – Assigning a value to a variable – Pausing for five seconds – Drawing a picture – Playing a song – Downloading and displaying a web page 26

Statements and Expressions A typical program needs both expressions and statements. Expressions are used to compute new data. Statements are used to do things that the user can see. A program without expressions does not compute anything. A program without statements does not produce any output (hard to imagine a use). 27

A Statement-Less Program Type this into a text file, and execute: 12+45*2 15 / 2 + 4**7 Each of these two lines of code computes something (so Python will spend the time needed to compute those values). However, the program shows nothing to the user. Thus, the computation has been wasted. 28

The print Statement Syntax: print exp_1, exp_2, …, exp_n – print is followed by 0 or more expressions, SEPARATED BY A COMMA. – print prints the RETURN VALUE of each expression. What will this line print? print 12+3 It will print 15, it will NOT just print the text "12+3" 29

Expressions Can Also Have Side- Effects Consider this line: radius_string = raw_input("Enter the radius of your circle: ") In the above line, the right side of the assignment operator contains this expression: raw_input("Enter the radius of your circle: ") This expression also has a side effect: it prints out a message. 30

Assignment Statements An assignment statement has this syntax: my_variable = expression What an assignment statement does is: – compute the return value of expression – associate that return value with the variable called my_variable. – from now on, my_variable is an expression whose return value is the value stored in my_variable. 31

The Assignment Operator In Python, we call the = sign the assignment operator. The assignment operator looks the same, but is not the same as the = sign in math. First difference: – in math, "a = 5" is the same as "5 = a". – In Python, "a = 5" is a valid piece of code assigning value 5 to variable a. "5 = a" is not valid code. 32

The Assignment Operator In Python, we call the = sign the assignment operator. The assignment operator looks the same, but is not the same as the = sign in math. Second difference: – in math, "a = a + 5" is nonsense (most of the time) – In Python, "a = a + 5" is a valid piece of code, and increments the value of a by 5. 33

The Assignment Operator In Python, we call the = sign the assignment operator. The assignment operator looks the same, but is not the same as the = sign in math. Third difference: – in math, "a + 5 = 7" is a well-defined equation. – In Python, "a + 5 = 7" is not a valid piece of code, because the left side of the assignment operator is NOT a variable name. 34

Variable Names Variable names must follow some simple rules: – must begin with a letter or an underscore. DO NOT start variable names with underscores for the time being. – can include letters, numbers and underscores but cannot start with a number. – are case sensitive, name and Name are different variables. – cannot be the same as a keyword Try in Python: print = 15 35

Choosing Variable Names Python does not care what names you use (as long as you follow the rules). However, descriptive variable names can make a huge difference in program readability, helping both others and yourself understand and debug your code. – You will probably be surprised by how hard it will be for yourself to read your own code a few days or weeks after you wrote it. 36

Examples of Assignments >>> xInt = 5 >>> yInt = >>> yInt = yInt + 7 NOTE: while evaluating the expression on the right side of the assignment operator, the OLD value of the variable is used. 37

Examples of Errors myInt + 5 = 7 7 = myInt + 5 print myInt=5 38

Examples of Errors myInt + 5 = 7 – Left side of assignment operator is NOT a variable name. 7 = myInt + 5 – Left side of assignment operator is NOT a variable name. print myInt=5 – Violates the print syntax: print must be followed by expressions, myInt=5 is NOT an expression. 39

Operators += -= *= /= a += 5 – is the same as: a = a + 5 a -= 5 – is the same as a = a – 5 a *= 5 – is the same as a = a * 5 a /= 5 – is the same as a = a / 5 40

The Notion of Syntactic Sugar If Python did not have +=, -=, *=, /=, it would not prevent us from writing any code. – We would just need to write slightly longer (but easier to read) lines. – The term "syntactic sugar" refers to elements of a programming language that are not vital, but simply allow somewhat shorter/more convenient alternative ways to write something. 41

Types In Python, every expression has a type. You can find the type of any expression using the type keyword >>> type(4) >>> type(10+12) For now, we care about three types: – integers (int), real numbers (float), strings (str) >>> a = 2.34 >>> type(a) >>> b = "hello" >>> type(b) 42

Types The int type: – Used to store integers, like 4, 0, -10. The float type: – Used to store real numbers, like , 1.0, 10.5 The str type: – Used to store strings, i.e., text, like: "hello" "today is Monday" the contents of an entire book 43

Why Are Types Important? The type of an expression specifies two things: – the internal structure of the expression (what kind of data it contains) – the kinds of operations that Python can perform on that expression 44

int vs. float A simple example where types make a difference: >>> 5/2 ??? >>> 5.0/2.0 ??? 45

int vs. float A simple example where types make a difference: >>> 5/2 2 >>> 5.0/ Why do these two lines produce different results? 46

int vs. float A simple example where types make a difference: >>> 5/2 2 >>> 5.0/ Why do we get two different results? – The first line performs integer division: the result is an integer and the remainder is discarded. 47

Automatic Type Assignment Python does not require you to pre‐define the type of a variable. – An important difference from Java, C++. What type a variable holds can change. – Nonetheless, knowing the type can be important for using the correct operations on a variable. 48

An Example of Type Conversion From the circles.py program: # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(radius_string) The int keyword is used to convert a string into an integer. 49

Type Conversion int(var_name) converts to an integer float(var_name) converts to a float str(var_name) converts to a string You should check these out: – int(2.1) → 2, int(‘2’) → 2, – int(‘2.1’) will fail – float(2) → 2.0, float(‘2.0’) → 2.0, – float(‘2’) → 2.0, float(2.0) → 2.0 – str(2) → ‘2’, str(2.0) → ‘2.0’, str(‘a’) → ‘a’ 50

Implicit Type Conversion What does 4/3.0 produce? 4 is an integer, 3.0 is a float. Python cannot perform arithmetic operations on mixed types. Python will automatically convert to the most informative type. – float is more informative than int, since converting from int to float does not lose information, converting from float to int loses the decimal digits. Thus, the result is

The + and += Operator on Strings >>> "hello" + "world" 'helloworld' >>> a = "good" >>> a += " morning" >>> a 'good morning' The + operator concatenates strings together into a single string. 52

Strings vs. Numerical Expressions >>> a = 12+3 >>> a 15 >>> a = "12+3" >>> a >>> 12+3 >>> print >>> print "12+3" 12+3 do not confuse a string (which is text) looking like a numerical expression with the numerical expression itself. 53

Assignments Using raw_input Expression Syntax: variable = raw_input(expression) Such a line does the following: – Prints the return value of the expression (this means that the expression must be evaluated). – Waits for the user to write some text and press – Stores the text typed by the user into variable. 54

Program Execution # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print "Circumference = ", circumference # compute and print the area area = (radius ** 2) * pi print "area = ", area Execute lines from top to bottom (exceptions apply, we will see soon). For each line, first evaluate the expressions that are encountered on that line, and then execute the statements (if any) that use those expressions. 55