# Lesson 03: Variables and Types Lesson 03: Variables and Types Attendance: Link: In Gitter.im | Code: ???? Class Chat: https://gitter.im/IST256/Fudge Participation http://ist256.participoll.com/ Agenda Youve Read: What is a variable? What is its Zybook Ch2 purpose? P4E Ch2 Different data types for variables. Type checks and conversions. Questions? Ask in Our Course Chat!

Print types with formatting Arithmetic expressions, arithmetic operators, and operands. https://gitter.im/IST256/Fudge 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. Youll Learn More That Way. Trust Us. You Have The Code Already. YOURS: Content\lessons\03\ MINE: https://github.com/IST256-classroom/fall2018-learn-pythonmafudge Connect Activity These statements are out of sequence, which letter represents the 3rd step in this program?: A.

B. C. D. print("Hello", name) fn=input("Enter First Name:") name = fn + " " + ln ln=input("Enter Last Name:") 17% (17) A 73% (74) 8% (8) 3%

(3) B C D 10 2 vote at ist256.participoll.com 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 variables 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 Name = "mike" Age = 45 Whats Happening "mike" 45 Name Age Variables are of a Specific Type

Type Purpose Numeric type integers only int float Numeric type floating point numbers bool True or False values Characters and text str Example 45 3.14 True 'Mike' Type Detection and Conversion Function type(n) int(n)

float(n ) str(n) What it does Get the type of n Convert n to type int Convert n to type float Example Convert n to type str str(4.0) '4.0' type(13) int int("45") 45 float(45) 45.0

Watch Me Code 1 Understanding Variables and Types! - Assignment - Variables of Different Types - Switching types - Using type() Check Yourself: Which Type 1? Match the Python code sample to its value. Python Code Sample Value str(34) A. 34 B. "34" C. int D. "34.0"

73% (77) 7% (7) A B 10% (10) 10% (11) C D

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

B C D 4 vote at ist256.participoll.com Python String Formatting Code Type %d %f %s Example Output

int print("%d" % 50) 50 float print("\$%.2f" % 4.5) \$4.50 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 Check Yourself: Formatting 1 Match each print() statement with formatting to

its output Python Print Statement Output print("\$%s" % 34) A. 34 B. 34.0 C. \$34 D. \$34.0 100 % (1) A B C

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

95% (81) 2% (2) 1% (1) A B C 1% (1) D

85 vote at ist256.participoll.com Programmatic Expressions Programmatic Expressions contain operators and operands. They evaluate to a value, preserving type: Examples: Value of X X X X X = = = =

2 + 2 4 2.0 + 2 4.0 "sh" + 'ip' ship 'hi' + 2 TypeError Arithmetic Operators Symbol + * / // % () What it does Adds two numbers or concatenates two strings Subtracts two numbers.

Multiplies two numbers Divides two numbers. Result is float Divides two numbers. Given quotient as int. Divides two numbers. Gives remainder as int. Force an order of operations Example 3 + 4 7 4 3 1 4 * 3 12 4 / 3 1.3333 13 // 3 4 13 % 3 1 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" What is the value of: type(int("1"+"4")/2) Place your response on gitter.im !!! Ill post the correct answer a few minutes after class!