Expressions, Statements, Variables, Assignments, Types

Size: px
Start display at page:

Download "Expressions, Statements, Variables, Assignments, Types"

Transcription

1 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

2 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

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

4 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( input("enter a number: ") yes 4

5 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( input("enter a number: ") yes, return value is a string of whatever we type. 5

6 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

7 How Can We Tell If Something is an Expression? "Thinking" way: understand what areexpressions 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

8 How Can We Tell If Something is an Expression? "Thinking" way: understand what areexpressions 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. c = raw_input("enter a number: ") expression 8

9 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

10 Trick Question Is this an expression? 12 minus 5 10

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

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

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

14 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

15 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

16 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

17 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

18 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 Highlighted in red you see some examples of expressions, there are moreexamples examples than the ones shown. Remember: Simple l expressions can be parts of more complicated expressions. # compute and print the area area = (radius ** 2) * pi print "area = ", area Expressions may consist of more simple expressions. 18

19 What Does Python Do With Expressions? Every time Pth 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

20 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

21 One Expression vs. Many Expressions Note: (kind of obvious, but I have seen many people make this mistake, itk which h 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

22 One Expression vs. Many Expressions Note: (kind of obvious, but I have seen many people make this mistake, itk which h 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 oepessosdo expressions NOT make aeup a single ge expression. The comma is NOT part of either expression. Yes, understanding code means understanding such details. 22

23 One Expression vs. Many Expressions Note: (kind of obvious, but I have seen many people make this mistake, itk which h is serious): Two (or more) expressions are NOT an expression. my_variable = Here, is a single expression, containing two more simple peepessos( expressions (41,and d3 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 memorylimitsof of the computer) on how large/complicated an expression can be. 23

24 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

25 Statements Consider this line of code: print "Circumference = ", circumference This line is NOT an expression (lh (although hit 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

26 Statements and Side Effects What a statement does is called a side effect. Examples of side effects: Pi 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

27 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

28 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 tti has been wasted. td 28

29 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

30 Expressions Can Also Have Side Consider this line: Effects 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( input("enter the radius ofyour circle: ") This expression also has a side effect: it prints out a message. 30

31 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

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. First difference: in math, "a = 5" is the same as "5 = a". In Pth Python, "a = 5" is a valid piece of code assigning i value 5 to variable a. "5 = a" is not valid code. 32

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. Second difference: in math, "a = a + 5" is nonsense (most of the time) In Pth Python, "a = a + 5" is a valid piece of code, and increments the value of a by 5. 33

34 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 Pth 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

35 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

36 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 othersand yourself understand and debug your code. You will probably bbl be surprised dby how hard dit will be for yourself to read your own code a few days or weeks after you wrote it. 36

37 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

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

39 Examples of Errors myint + 5 = 7 Left side of assignment operator is NOT a variable name. 7 = myint + 5 Leftside 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

40 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

41 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. Theterm "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

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

43 Types The int type: Used to store integers, like 4, 0, 10. Thefloat type: Used to store real numbers, like 13.34, 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

44 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

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

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

47 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 The first line performs integer division: the result is an integer and the remainder is discarded. 47

48 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

49 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

50 Type Conversion int(var_name) 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, 20 float( 2.0 ) 2.0, 20 float( 2 ) 2.0, float(2.0) 2.0 str(2) 2, str(2.0) 2.0, str( a ) str(a) a 50

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

52 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

53 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

54 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 <ENTER> Stores the text typed by the user into variable. 54

55 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

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Programming I. Lecture#12 Spring 2015 Algorithms and Programming I Lecture#12 Spring 2015 Think Python How to Think Like a Computer Scientist By :Allen Downey Installing Python Follow the instructions on installing Python and IDLE on your

More information

T H E I N T E R A C T I V E S H E L L

T H E I N T E R A C T I V E S H E L L 3 T H E I N T E R A C T I V E S H E L L The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. Ada Lovelace, October 1842 Before

More information

Variables, expressions and statements

Variables, expressions and statements Variables, expressions and statements 2.1. Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. The values we have seen so far are 2 (the

More information

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an  . Michigan State University CSE 231, Fall 2013 CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?

More information

Expressions and Variables

Expressions and Variables Expressions and Variables Expressions print(expression) An expression is evaluated to give a value. For example: 2 + 9-6 Evaluates to: 5 Data Types Integers 1, 2, 3, 42, 100, -5 Floating points 2.5, 7.0,

More information

Act like a code monkey Coding Basics

Act like a code monkey Coding Basics Act like a code monkey Coding Basics Announcement Homework 1 grade is posted If you believe there is an error in grading (assignments or quizzes), you may request a regrading within one week of receiving

More information

Variables, Types, Operations on Numbers

Variables, Types, Operations on Numbers Variables, Types, Operations on Numbers CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Updated 9/6/16 1 Summary Variable declaration, initialization,

More information

Exercise: Inventing Language

Exercise: Inventing Language Memory Computers get their powerful flexibility from the ability to store and retrieve data Data is stored in main memory, also known as Random Access Memory (RAM) Exercise: Inventing Language Get a separate

More information

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2 Python for Analytics Python Fundamentals RSI Chapters 1 and 2 Learning Objectives Theory: You should be able to explain... General programming terms like source code, interpreter, compiler, object code,

More information

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington First Programs CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Output System.out.println( ) prints out something. System.out.println is the first

More information

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1 Chapter 2 Input, Processing, and Output Fall 2016, CSUS Designing a Program Chapter 2.1 1 Algorithms They are the logic on how to do something how to compute the value of Pi how to delete a file how to

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

Decisions, Decisions. Testing, testing C H A P T E R 7

Decisions, Decisions. Testing, testing C H A P T E R 7 C H A P T E R 7 In the first few chapters, we saw some of the basic building blocks of a program. We can now make a program with input, processing, and output. We can even make our input and output a little

More information

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington First Programs CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Output System.out.println( ) prints out something. System.out.println is the first

More information

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington First Programs CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Output System.out.println( ) prints out something. System.out.println is the first

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

Programming Fundamentals and Python

Programming Fundamentals and Python Chapter 2 Programming Fundamentals and Python This chapter provides a non-technical overview of Python and will cover the basic programming knowledge needed for the rest of the chapters in Part 1. It contains

More information

Lecture 3. Input, Output and Data Types

Lecture 3. Input, Output and Data Types Lecture 3 Input, Output and Data Types Goals for today Variable Types Integers, Floating-Point, Strings, Booleans Conversion between types Operations on types Input/Output Some ways of getting input, and

More information

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

More information

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu) I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications

More information

Python Intro GIS Week 1. Jake K. Carr

Python Intro GIS Week 1. Jake K. Carr GIS 5222 Week 1 Why Python It s simple and easy to learn It s free - open source! It s cross platform IT S expandable!! Why Python: Example Consider having to convert 1,000 shapefiles into feature classes

More information

>>> * *(25**0.16) *10*(25**0.16)

>>> * *(25**0.16) *10*(25**0.16) #An Interactive Session in the Python Shell. #When you type a statement in the Python Shell, #the statement is executed immediately. If the #the statement is an expression, its value is #displayed. #Lines

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Variables and Constants

Variables and Constants 87 Chapter 5 Variables and Constants 5.1 Storing Information in the Computer 5.2 Declaring Variables 5.3 Inputting Character Strings 5.4 Mistakes in Programs 5.5 Inputting Numbers 5.6 Inputting Real Numbers

More information

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

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018 Python Input, output and variables Lecture 23 COMPSCI111/111G SS 2018 1 Today s lecture What is Python? Displaying text on screen using print() Variables Numbers and basic arithmetic Getting input from

More information

CSI Lab 02. Tuesday, January 21st

CSI Lab 02. Tuesday, January 21st CSI Lab 02 Tuesday, January 21st Objectives: Explore some basic functionality of python Introduction Last week we talked about the fact that a computer is, among other things, a tool to perform high speed

More information

Introduction to programming with Python

Introduction to programming with Python Introduction to programming with Python Ing. Lelio Campanile 1/61 Main Goal - Introduce you to programming - introduce you to the most essential feature of python programming 2/61 Before to start The name

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016 CSC 120 Computer Science for the Sciences Week 1 Lecture 2 UofT St. George January 11, 2016 Introduction to Python & Foundations of computer Programming Variables, DataTypes, Arithmetic Expressions Functions

More information

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT Python The way of a program Srinidhi H Asst Professor Dept of CSE, MSRIT 1 Problem Solving Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution

More information

Programming with Python

Programming with Python Programming with Python Dr Ben Dudson Department of Physics, University of York 21st January 2011 http://www-users.york.ac.uk/ bd512/teaching.shtml Dr Ben Dudson Introduction to Programming - Lecture 2

More information

1/11/2010 Topic 2: Introduction to Programming 1 1

1/11/2010 Topic 2: Introduction to Programming 1 1 Topic 2: Introduction to Programming g 1 1 Recommended Readings Chapter 2 2 2 Computer Programming Gain necessary knowledge of the problem domain Analyze the problem, breaking it into pieces Repeat as

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter Variables, Expressions, and Statements Chapter 2 Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/.

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

CS1110 Lab 1 (Jan 27-28, 2015)

CS1110 Lab 1 (Jan 27-28, 2015) CS1110 Lab 1 (Jan 27-28, 2015) First Name: Last Name: NetID: Completing this lab assignment is very important and you must have a CS 1110 course consultant tell CMS that you did the work. (Correctness

More information

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python ENGG1811 UNSW, CRICOS Provider No: 00098G W4 Computers have changed engineering http://www.noendexport.com/en/contents/48/410.html

More information

Lab 1: Input, Processing, and Output This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design.

Lab 1: Input, Processing, and Output This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design. Starting Out with Programming Logic and Design 1 Lab 1: Input, Processing, and Output This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design. Lab 1.1 Algorithms Name: Critical Review

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu) I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications

More information

Week - 01 Lecture - 04 Downloading and installing Python

Week - 01 Lecture - 04 Downloading and installing Python Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 04 Downloading and

More information

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28)   First Name: Last Name: NetID: CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) http://www.cs.cornell.edu/courses/cs1110/2016sp/labs/lab01/lab01.pdf First Name: Last Name: NetID: Goals. Learning a computer language is a lot like learning

More information

Math Day 2 Programming: How to make computers do math for you

Math Day 2 Programming: How to make computers do math for you Math Day 2 Programming: How to make computers do math for you Matt Coles February 10, 2015 1 Intro to Python (15min) Python is an example of a programming language. There are many programming languages.

More information

Project 5 - The Meta-Circular Evaluator

Project 5 - The Meta-Circular Evaluator MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 2005 Project 5 - The Meta-Circular

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions

More information

COMP 202. Java in one week

COMP 202. Java in one week COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

More information

Getting Started with Python

Getting Started with Python Fundamentals of Programming (Python) Getting Started with Python Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

Python for Non-programmers

Python for Non-programmers Python for Non-programmers A Gentle Introduction 1 Yann Tambouret Scientific Computing and Visualization Information Services & Technology Boston University 111 Cummington St. yannpaul@bu.edu Winter 2013

More information

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007 CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007 Course Web Site http://www.nps.navy.mil/cs/facultypages/squire/cs2900 All course related materials will be posted

More information

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013 Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements Topic 4 Variables Once a programmer has understood the use of variables, he has understood the essence of programming -Edsger Dijkstra What we will do today Explain and look at examples of primitive data

More information

CMSC 201 Computer Science I for Majors

CMSC 201 Computer Science I for Majors CMSC 201 Computer Science I for Majors Lecture 02 Intro to Python Syllabus Last Class We Covered Grading scheme Academic Integrity Policy (Collaboration Policy) Getting Help Office hours Programming Mindset

More information

Expressions and Casting

Expressions and Casting Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Welcome to Python 3. Some history

Welcome to Python 3. Some history Python 3 Welcome to Python 3 Some history Python was created in the late 1980s by Guido van Rossum In December 1989 is when it was implemented Python 3 was released in December of 2008 It is not backward

More information

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners Getting Started Excerpted from Hello World! Computer Programming for Kids and Other Beginners EARLY ACCESS EDITION Warren D. Sande and Carter Sande MEAP Release: May 2008 Softbound print: November 2008

More information

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

Topic 2: Introduction to Programming

Topic 2: Introduction to Programming Topic 2: Introduction to Programming 1 Textbook Strongly Recommended Exercises The Python Workbook: 12, 13, 23, and 28 Recommended Exercises The Python Workbook: 5, 7, 15, 21, 22 and 31 Recommended Reading

More information

ENGR 102 Engineering Lab I - Computation

ENGR 102 Engineering Lab I - Computation ENGR 102 Engineering Lab I - Computation Week 03: Data Types and Console Input / Output Introduction to Types As we have already seen, 1 computers store numbers in a binary sequence of bits. The organization

More information

Variables, Expressions, and Statements

Variables, Expressions, and Statements Variables, Expressions, and Statements Chapter 2 Python for Informatics: Exploring Information www.pythonlearn.com Constants Fixed values such as numbers, letters, and strings are called constants because

More information

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day Instructor: Jason Yoon Website: Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

More information

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas If you have your own PC, download and install a syntax-highlighting text editor and Python

More information

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1 The Three Rules Chapter 1 Beginnings Rule 1: Think before you program Rule 2: A program is a human-readable essay on problem solving that also executes on a computer Rule 3: The best way to improve your

More information

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi CMPT 120 Basics of Python Summer 2012 Instructor: Hassan Khosravi Python A simple programming language to implement your ideas Design philosophy emphasizes code readability Implementation of Python was

More information

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming

More information

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed CS 150 Introduction to Computer Science I Data Types Today Last we covered o main function o cout object o How data that is used by a program can be declared and stored Today we will o Investigate the

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables COMP-202 Unit 2: Java Basics CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables Tutorial 0 Help with setting up your computer to compile and

More information

MIT AITI Python Software Development

MIT AITI Python Software Development MIT AITI Python Software Development PYTHON L02: In this lab we practice all that we have learned on variables (lack of types), naming conventions, numeric types and coercion, strings, booleans, operator

More information

CSCI 121: Anatomy of a Python Script

CSCI 121: Anatomy of a Python Script CSCI 121: Anatomy of a Python Script Python Scripts We start by a Python script: A text file containing lines of Python code. Each line is a Python statement. The Python interpreter (the python3 command)

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information

Our Strategy for Learning Fortran 90

Our Strategy for Learning Fortran 90 Our Strategy for Learning Fortran 90 We want to consider some computational problems which build in complexity. evaluating an integral solving nonlinear equations vector/matrix operations fitting data

More information

Have examined process Creating program Have developed program Written in C Source code

Have examined process Creating program Have developed program Written in C Source code Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives

More information

Fundamentals of Programming (Python) Getting Started with Programming

Fundamentals of Programming (Python) Getting Started with Programming Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

Chapter 2 C++ Fundamentals

Chapter 2 C++ Fundamentals Chapter 2 C++ Fundamentals 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Reuse existing code in your programs with #include Obtain input data from the user

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s Intro to Python Python Getting increasingly more common Designed to have intuitive and lightweight syntax In this class, we will be using Python 3.x Python 2.x is still very popular, and the differences

More information

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif Input, Processing and Output Bonita Sharif 1 Review A program is a set of instructions a computer follows to perform a task The CPU is responsible for running and executing programs A set of instructions

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review

More information

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas If you have your own PC, download and install a syntax-highlighting text editor and Python

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

Choose the file menu, and select Open. Input to be typed at the Maple prompt. Output from Maple. An important tip.

Choose the file menu, and select Open. Input to be typed at the Maple prompt. Output from Maple. An important tip. MAPLE Maple is a powerful and widely used mathematical software system designed by the Computer Science Department of the University of Waterloo. It can be used for a variety of tasks, such as solving

More information

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn Problem Write and test a Scheme program to compute how many days are spanned by two given days. The program will include a procedure

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Python Unit

Python Unit Python Unit 1 1.1 1.3 1.1: OPERATORS, EXPRESSIONS, AND VARIABLES 1.2: STRINGS, FUNCTIONS, CASE SENSITIVITY, ETC. 1.3: OUR FIRST TEXT- BASED GAME Python Section 1 Text Book for Python Module Invent Your

More information

Topic 3: Fractions. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

Topic 3: Fractions. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra Topic : Fractions Topic Integers Topic Decimals Topic Fractions Topic Ratios Topic Percentages Duration / weeks Content Outline PART (/ week) Introduction Converting Fractions to Decimals Converting Decimals

More information

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik SAMS Programming A/B Lecture #1 Introductions July 3, 2017 Mark Stehlik Outline for Today Overview of Course A Python intro to be continued in lab on Wednesday (group A) and Thursday (group B) 7/3/2017

More information

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors CMSC201 Computer Science I for Majors Lecture 12 Tuples All materials copyright UMBC and Dr. Katherine Gibson unless otherwise noted Modularity Meaning Benefits Program design Last Class We Covered Top

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2 Basic Concepts Computer Science Computer - Science - Programming history Algorithms Pseudo code 2013 Andrew Case 2 Basic Concepts Computer Science Computer a machine for performing calculations Science

More information

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications Hello World! Computer Programming for Kids and Other Beginners by Warren Sande and Carter Sande Chapter 1 Copyright 2009 Manning Publications brief contents Preface xiii Acknowledgments xix About this

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

CS 112: Intro to Comp Prog

CS 112: Intro to Comp Prog CS 112: Intro to Comp Prog Lecture Review Data Types String Operations Arithmetic Operators Variables In-Class Exercises Lab Assignment #2 Upcoming Lecture Topics Variables * Types Functions Purpose Parameters

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Assignment 1 Assignment 1 posted on WebCt. It will be due January 21 st at 13:00 Worth 4% Last Class Input and Output

More information