Lesson 03: Variables and Types

Slides:



Advertisements
Similar presentations
Python November 14, Unit 7. Python Hello world, in class.
Advertisements

Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
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
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 2 Variables.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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.
Input, Output and Variables GCSE Computer Science – Python.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Math operations 9/19/16.
Numbers and arithmetic
Lesson 06: Functions Class Participation: Class Chat:
CST 1101 Problem Solving Using Computers
Chapter 2 Variables.
Numbers and Arithmetic
Topics Designing a Program Input, Processing, and Output
Lesson 07: Strings Class Chat: Attendance: Participation
Expressions.
Lecture 4: Expressions and Variables
Building Java Programs
Lesson 02: Introduction to Python
Lesson 05: Iterations Class Chat: Attendance: Participation
Design & Technology Grade 7 Python
Variables, Expressions, and IO
Lesson 07: Strings Topic: Introduction to Programming, Zybook Ch 6, P4E Ch 6. Slides on website.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Lesson 04: Conditionals Class Chat: Attendance: Participation
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Learning Outcomes –Lesson 4
Building Java Programs
Building Java Programs
Chapter 2 Variables.
Lesson 06: Functions Class Chat: Attendance: Participation
Lesson 09: Lists Class Chat: Attendance: Participation
Building Java Programs
Lesson 03: Variables and Types
Building Java Programs Chapter 2
Lesson 08: Files Class Chat: Attendance: Participation
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Lesson 10: Dictionaries Class Chat: Attendance: Participation
Building Java Programs
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Lecture 4: Expressions and Variables
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Building Java Programs
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Lesson 02: Introduction to Python
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.
Building Java Programs
Lesson 07: Strings Class Chat: Attendance: Participation
Hardware is… Software is…
Building Java Programs
IST256 : Applications Programming for Information Systems
Data Types and Expressions
Presentation transcript:

Lesson 03: Variables and Types Attendance: Link: In Gitter.im | Code: ???? Class Chat: https://gitter.im/IST256/Fudge Participation http://ist256.participoll.com/

Questions? Ask in Our Course Chat! Agenda What is a variable? What is its purpose? Different data types for variables. Type checks and conversions. Print types with formatting Arithmetic expressions, arithmetic operators, and operands. You’ve Read: Zybook Ch2 P4E Ch2 https://gitter.im/IST256/Fudge Questions? Ask in Our Course Chat!

Before We start: Where Is The Code!? Today I Will Write Code In Class. Please DO NOT Try To Type Along With Me! Watch Me Code. You’ll Learn More That Way. Trust Us. You Have The Code Already. Content\lessons\03\Watch-Me-Code Content\lessons\03\End-To-End-Examples

Connect Activity These statements are out of sequence, which letter represents the 3rd step in this program?: print("Hello", name) fn=input("Enter First Name:") name = fn + " " + ln ln=input("Enter Last Name:") A B C D

Variables Variables are named areas of computer memory for storing data. The name can be anything but should make symbolic sense to the programmer. We write to the variable’s memory location with the assignment statement (=) We read from the variable by calling its name. Variable names must begin with a letter or _ and must only contain letters, numbers or _.

Variables, Types and Assignment Python Code What’s Happening Name = "mike" Age = 45 "mike" Name 45 Age

Variables are of a Specific Type Purpose Example int Numeric type integers only 45 float Numeric type floating point numbers 3.14 bool True or False values True str Characters and text 'Mike'

Type Detection and Conversion Function What it does Example type(n) Get the type of n type(13)  int int(n) Convert n to type int int("45")  45 float(n) Convert n to type float float(45)  45.0 str(n) Convert n to type str str(4.0)  '4.0'

Watch Me Code 1 Understanding Variables and Types! Assignment Variables of Different Types Switching types Using type() Write this code in a Jupyter Notebook to demonstrate variables and types name = "Tony" age = 43 wage = 12.50 happy_employee = True print ("my name is",name," I am", age, "years old. I make ", wage, "Am I happy? ", happy_employee) GROK TYPES type(wage) etc… EXPLAIN THAT YOU CAN ALSO SWITCH TYPES OF VARIABLES Happy_employee = “You betcha!”

Check Yourself: Matching Types Match each Python code sample to its value. Python Code Sample Value type(34) str(34) float(34) int(34) 34 "34" int "34.0"

Check Yourself: Which Type 1? Match the Python code sample to its value. Python Code Sample Value str(34) 34 "34" int "34.0" A B C D

Check Yourself: Which Type 2? Match the Python code sample to its value. Python Code Sample Value type(34) 34 "34" int "34.0" A B C D

Python String Formatting Code Type Example Output %d int print("%d" % 50) 50 %f float print("$%.2f" % 4.5) $4.50 %s str print("[%s]" % "mike") [mike] You can put an integer between the % and code for spacing: %5d  Use 5 spaces for the int, align right %-5d  Use 5 spaces for the int, align left

Watch Me Code 2 Python String Formatting Format codes Spacing Alignment Write this code in a Jupyter Notebook to demonstrate variables and types name = "Tony" age = 43 wage = 12.50 happy_employee = True print ("my name is",name," I am", age, "years old. I make ", wage, "Am I happy? ", happy_employee) GROK TYPES type(wage) etc… EXPLAIN THAT YOU CAN ALSO SWITCH TYPES OF VARIABLES Happy_employee = “You betcha!”

Check Yourself: Formatting Match each print() statement to its output Python Print Statement Output print("$%s" % 34) print("%.1f" % 34) print("%d" % 34 ) 34 34.0 $34

Check Yourself: Formatting 1 Match each print() statement with formatting to its output Python Print Statement Output print("$%s" % 34) 34 34.0 $34 $34.0 A B C D

Check Yourself: Formatting 2 Match each print() statement with formatting to its output Python Print Statement Output print("%.1f" % 34) 34 34.0 $34 $34.0 A B C D

Programmatic Expressions Programmatic Expressions contain operators and operands. They evaluate to a value, preserving type: Examples: Value of X X = 2 + 2 4 X = 2.0 + 2 4.0 X = "sh" + 'ip' “ship” X = 'hi' + 2 TypeError

Arithmetic Operators Symbol What it does Example + Adds two numbers or concatenates two strings 3 + 4  7 - Subtracts two numbers. 4 – 3  1 * Multiplies two numbers 4 * 3  12 / Divides two numbers. Result is float 4 / 3  1.3333 // Divides two numbers. Given quotient as int. 13 // 3  4 % Divides two numbers. Gives remainder as int. 13 % 3  1 () Force an order of operations 2 * (1 + 4)  10

End-To-End Example The Pay-Rate Calculator: Write a program to prompt for hourly rate, and hours worked for the week as inputs Then calculates the total pay as output. Then prompts for tax rate as input, and outputs net pay.

Conclusion Activity "1 Question Challenge" type(int("1"+"4")/2) What is the value of: type(int("1"+"4")/2) Place your response on gitter.im !!! I’ll post the correct answer a few minutes after class!