Variables, Types, Expressions Intro2CS – week 1b 1.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
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 to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Recitation 1 Programming for Engineers in Python.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Introduction to Python
Munster Programming Training
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Input, Output, and Processing
Variables and Expressions CMSC 201 Chang (rev )
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to Programming with RAPTOR
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
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.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Variables, Expressions and Statements
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Original Source : and Problem and Problem Solving.ppt.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CSC 107 – Programming For Science. Announcements.
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.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
GCSE Computing: Programming GCSE Programming Remembering Python.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction Python Expressions, Variables & Operators Intro2CS – week 1 1.
Tirgul 2 Basic programming. Overview Variables Types : int, float, string User input Functions with input and output The Boolean type and Boolean operations.
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.
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Input and Output Upsorn Praphamontripong CS 1110
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction to Python
Topic: Python’s building blocks -> Statements
Variables, Expressions, and IO
Building Java Programs Chapter 2
Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
15-110: Principles of Computing
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Terminal-Based Programs
General Computer Science for Engineers CISC 106 Lecture 03
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Chapter 1 c++ structure C++ Input / Output
Data Types and Expressions
Introduction to Python
Presentation transcript:

Variables, Types, Expressions Intro2CS – week 1b 1

Functions: reminder 2 A multi-line comment. Use these to document all functions you write.

The call stack def a(): print("start a()") b() c() print("fin a()") def b(): print("start b()") d() print("fin b()") def c(): print("start c()") d() print("fin c()") def d(): print("start and fin d()") 3 Function a() Function b() Function d() Line 1 Line 2 Line 3 Line 4 Line 1 Line 2 Line 3 >>> a() start a() start b() start and fin d() fin b() start c() start and fin d() fin c() fin a() Function c() Line 1 Line 2 Line 3 a() b() c()

Error messages and the call stack. When an error occurs within some function, the error message specifies the state of excution: 4

5

Defining Functions Functions must be defined before they are used. 6 a() #ERROR: a() is not defined yet. def a(): # not executed until a() called: return b() #Okay. a() #ERROR! Because b() isn’t defined. def b(): return 1 a() #This is Okay.

Types Every value or object in python has a type “hi”, -34, 0.2 and True are called literal values. There are many other types… “hi”, ‘hi’, “””hi””” string Text. A string of characters. -34int Integers … -2,-1,0,1,2,… 0.2float Approximate real numbers True, FalseboolTruth values 7

Operators The symbols * + / are called operators Use them to compose more complex expressions Operators work differently on different types: 8

A built in function type() can check the type for you: 9

List of operators Arithmetic operators: + - * / ** % // (The last 3 are exponent, modulus, floor division) Comparison operators: == != > = <= Logical operators: and or not There are more operators, that we will cover later (e.g., assignment operators, bitwise operators) 10

Logical operators XYX or YX and Y False TrueFalseTrueFalse True False True Xnot X FalseTrue False 11

Example What should this expression evaluate to? 3+4 == 14//2 and not 7 < == 14//2 and not 7 < == 7 and not 7 < 4.5 True and not 7 < 4.5 True and not False True and True True 12

Type Conversion Functions We can attempt to convert between types using built in functions 13

Variables We can assign values to a variable = Expression is evaluated, then result is assigned. Variable names can be made of letters, digits, ‘_’ (but must not start with digit). Choose meaningful names! x = 3 print(x) y = 2+x print(y) 14

Variables can be reassigned (even to different types) The previous value is gone. Replaced by a different one. The output: number = 2 print(number) number = “hello” print(number) 2 hello 15

Assignments are not equations! Note the difference between =, == (assignment vs. comparison operator) What does this do? x = 2 x = x+1 y = 3 x = y == x = 5 makes sense in Python (but not in math) Not legal python syntax! (but makes sense in math) 16

What is the output? Does the second assignment to x change y’s value? x = 2 y = x x = x+1 print(x, y) 17

Another Example Output: 18 #pretty print some text: text = “hello” length = len(text) print("*" * (length+4)) print("*",text,"*") print("*" * (length+4)) ********** * Hello! * **********

Input from the user We can write a program that will accept input from its user Using the function input([prompt]) This will pause the program and wait for input from the user. The input is returned as a string A possible run: name = input("Please enter your name: ") print("Hello", name) Please enter your name: Joe Hello Joe 19

Recap Expressions are composed of combinations of variables, operators, literals, and functions Expressions are evaluated by the interpreter whenever encountered. We’ve learned that values in Python have a type – We’ve seen int, string, float, bool – Operators work differently, depending on types We’ve seen two types of statements: Calls to functions: print( ) Assignment: = 20

A program with a bug width = input("Enter rectangle's width: ") height = input("Enter rectangle's height: ") area = width * height print("The area is: ", area) Enter rectangle's width: 4 Enter rectangle's height: 3 Traceback (most recent call last): File "C:/Users/avivz/Desktop/tmp/expressions.py", line 4, in area = width * height TypeError: can't multiply sequence by non-int of type 'str' 21

Actual bug found in the Harvard mark II (1947) 22

Correcting the problem width = input("Enter rectangle's width: ") height = input("Enter rectangle's height: ") area = float(width) * float(height) print("The area is: ", area) Enter rectangle's width: 4 Enter rectangle's height: 3 The area is: 12.0 input() gave us strings that we later tried to multiply. Convert the input to float before doing so. 23

Different errors in code Intent error: the program works, but does something we did not mean Syntax error: the interpreter stops execution since we mistyped something in the program. – Found just before, or during the run Runtime errors: – Something unexpected crashes the program, like applying an illegal operation: 24

Other Program Examples Read the radius of a circle from the user, and print its area and circumference. – What happens if the number we receive is negative? – What happens if we receive a string instead? 25