UNIT 1. Variables Binary Math

Similar documents
Computers in Engineering COMP 208. A Brief History. Mechanical Calculators. A Historic Perspective Michael A. Hawker

Computers in Engineering COMP 208

Overview of a computer

Welcome to COSC Introduction to Computer Science

Chapter 1. The Big Picture

1: History, Generation & Classification. Shobhanjana Kalita, Dept. of CSE, Tezpur University

A Brief History of Computer Science

You Will Need Floppy Disks for your labs!

Algorithm: Program: Programming: Software: Hardware:

EVOLUTION OF COMPUTERS. In the early years, before the computer was invented, there are several inventions of counting machines.

Computer Basics. Computer Technology

(History of Computers) Lecture # 03 By: M.Nadeem Akhtar. Lecturer. URL:

Welcome to COS151! 1.1

Computer History CSCE 101

Babbage s computer 1830s Boolean logic 1850s. Hollerith s electric tabulator Analog computer 1927 EDVAC 1946 ENIAC

A Brief History of Computer Science. David Greenstein Monta Vista High School, Cupertino, CA

Expressions and Variables

Chapter 2 HISTORICAL DEVELOPMENT OF COMPUTERS

Part (01) Introduction to Computer

History of Computing. Slides from NYU and Georgia Tech

Great Inventions written by Bob Barton

Chapter 1 History & Hardware

Chapter 1: An Introduction to Computer Science. Invitation to Computer Science, C++ Version, 6-th Edition

Describe the layers of a computer system

In this chapter, you will learn about: The definition of computer science. Algorithms. Invitation to Computer Science, C++ Version, Third Edition

Computer Systems. Hardware, Software and Layers of Abstraction

Copyright 2012 Pearson Education, Inc. Publishing as Prentice Hall

HISTORY OF COMPUTING

Python Programming Exercises 1

CS 1 Notes 1 - Early Computing and 2 - Electronic Computing

Evolution of the Computer

Computer Architecture. Prologue. Topics Computer Architecture. Computer Organization. Organization vs. Architecture. History of Computers

The Generations of Computers

1.The First Instrument known in the history of computers was. a) Pascal s adding machine b) Napier s bones c) Abacus d) Analytical Engine

CS101 Lecture 29: Brief History of Computing

Programming with Python

Introduction to Computer Systems

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

Fundamentals of Python: First Programs. Chapter 1: Introduction Modifications by Mr. Dave Clausen

HISTORY OF COMPUTERS HISTORY OF COMPUTERS. Mesleki İngilizce - Technical English. Punch Card. Digital Data. II Prof. Dr. Nizamettin AYDIN.

Monday, January 27, 2014

Fundamental concepts of Information Technology

ENG 101 Lesson -6. History of Computers

Introduction to Computer Science. What is Computer Science?

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS


4. History of computers and applications

An Introduction to Computer Science CS 8: Introduction to Computer Science Lecture #2

CMSC 201 Computer Science I for Majors

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

Computer Evolution. Budditha Hettige. Department of Computer Science

CHAPTER 1 Introduction

COMP 102: Computers and Computing Lecture 1: Introduction!

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Computer Evolution. Computer Generation. The Zero Generation (3) Charles Babbage. First Generation- Time Line

Machine Architecture and Number Systems

Variable and Data Type I

Variable and Data Type I

Fundamentals of Digital Computers The mechanical computer age began with the advent of the abacus in 500 B.C by Babylonians. The abacus, which was

MACHINE LEVEL REPRESENTATION OF DATA

Machine Architecture and Number Systems CMSC104. Von Neumann Machine. Major Computer Components. Schematic Diagram of a Computer. First Computer?

A (BRIEF) HISTORY OF COMPUTING. By Dane Paschal

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Jython. secondary. memory

History of Modern Computing Lesson 1

COURSE OVERVIEW. Introduction to Computer Engineering 2015 Spring by Euiseong Seo

MICROPROCESSOR SYSTEM DESIGN

HIGHER SECONDARY FIRST YEAR 2 MARK & 5 MARK NOTES CHAPTER 1 1. INTRODUCTION TO COMPUTER

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS 1301 Exam 1 Fall 2009

CHAPTER 1 Introduction

CS 1301 Exam 1 Answers Fall 2009

SSRVM Content Creation Template

The float type and more on variables FEB 6 TH 2012

EEM336 Microprocessors I. Introduction to the Microprocessor and Computer

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

Variables, expressions and statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

Angel International School - Manipay 1 st Term Examination November, 2015 ICT

PROGRAMMING LANGUAGE PARADIGMS & THE MAIN PRINCIPLES OF OBJECT-ORIENTED PROGRAMMING

7. History of computers and applications

The C++ Language. Arizona State University 1

An Introduction to Python for KS4!

Computer Organization CS 206T

Chapter 2. The History and Development of Computers

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

I1100 Introduction to Computer Science Semester: 1 Academic Year: 2018/2019 Credits: 3 (30 hours) Dr. Antoun Yaacoub

The trusted, student-friendly online reference tool. Name: Date:

Objectives. In this chapter, you will:

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

Chapter 1: Introduction to Computers

An Introduction to Computer Science CS 8: Introduction to Computer Science, Winter 2018 Lecture #2

Chapter 1: Introduction to Computers. In this chapter you will learn about:

CS 102 Lab 3 Fall 2012

Reserved Words and Identifiers

CS177 Python Programming. Recitation 2 - Computing with Numbers

Ex: If you use a program to record sales, you will want to remember data:

What did we talk about last time? Examples switch statements

Outline. Data and Operations. Data Types. Integral Types

Transcription:

UNIT 1 Variables Binary Math

DAY 1 History of Computers Variables and Assignment

I can.. Identify important inventions and people in the history of computers. Explain the purpose of a variable in computer programming. Assign a variable in Python. Use variables in print statements

The History of Computers Main Periods in History Early Information Processors First Generation Computers Second Generation Computers Third Generation Computers Fourth Generation Computers Fifth Generation Computers Sixth Generation Computers

Early Information Processors Most of the early information processing machines were used to do arithmetic

Abacus Used in ancient times for addition Used as early as 5000 BC Doesn t actually count helps human calculate by remembering what has already been counted.

Printing Press Invented by Johann Gutenberg Invented in 1458 Wooden/metal pressable letters with ink when pressure applied to paper or cloth the machine would print

Napier s Bones Invented in 1617 Numbered rods which can be used to perform multiplication on any number 2-9

Slide Rule Invented in 1632 Invented by William Oughtred Primarily used for multiplication and division

Blaise Pascal (1623 1662) Developed a calculator called the Pascaline in 1641 Operated by turning dials It could only perform addition

Wilhelm von Leibniz (1646-1716) Developed a calculator in 1673 called the Leibniz Wheel Operated by turning a crank Could perform all four standard operations (+, -, x, )

Jacquard Loom Invented in 1801 Processes information by using punched cards to control a pattern woven into clothing materials Predecessor of the player piano

Charles Babbage (1791-1871) Known as the Father of the computer

Difference Engine (By Babbage) Invented in 1823 Designed to produce math tables Mechanical device powered by steam Abandoned the project because of difficulty making the machine's small parts

Analytic Engine (again Babbage) In 1833 Babbage had a better idea Could do ANY mathematical calculation Controlled using punched cards Also powered by steam He never finished it again limitations with machine parts However his design was the basis for modern computers hence the title Father of Computers

Lady Ada Lovelace (Ada Augusta Byron) Designed a sample program for Babbage s second machine on punched cards Though the program was never run she is known as the world s First Computer Programmer

George Bool (1815-1964) Used 1 to describe the value of TRUE and 0 for FALSE As a result boolean logic and boolean symbols are named after him

Herman Hollerith (1860-1929) Developed a punched card tabulating system for the 1890 US census Later left the census to create his own tabulating machine company which became known the Internation Business Machines (IBM) Company

Mark I Developed in 1944 (during WWI Manhattan Project) Based on Babbage s idea Mechanical device powered by electricity Controlled by punch cards

Mark 1 (Pictures)

Now Back to Python!

Data Types Data = another word for information Data Type: A particular type of piece of information. Different types of data have a different Data Type Python behaves differently depending on the type of information (data) it is working with.

Data Types Integers 9 34-5 Floats 23.45-963.2 12.0 Strings Hello This is a string 7+2 **There are more we will see in the future but we ll start with these three **

Why do we need data types? Data types tell the computer how much memory to use to store the value in memory - be efficient with memory use! Data types tell the computer the types of functions it can perform on the data - perform math operations on numbers (not strings)!

Data Types and Memory name James

Variables Variables are how we STORE information The name of a LOCATION in the computer s RAM Like a Mail Box: Has an address (location) and holds information for us!

Variables Can hold ONE value at a time When a new value is introduced the old value is ERASED What type of data a variable holds depends on the first data type it holds Declare a variable has a specific type

Variable Naming Rules Names of variables are also called Identifiers Every name MUST start with a letter or underscore ( _ ) Can only use a combination of letters, numbers, and underscores throughout the name CANNOT contain spaces ARE case sensitive (upper and lowercase letters are treated as different) length and Length are DIFFERENT variable names Should be DESCRIPTIVE (ex: sum representing a variable that will store the result of addition)

Valid or Invalid names? For each decide if the variable name is VALID or INVALID if it is INVALID, explain what rule the name is breaking A $Fred X-Y AZ sum student grade Dice_1 7G _Wisconsin U_W_Madison Muk-town A1

Valid or Invalid names? For each decide if the variable name is VALID or INVALID if it is INVALID, explain what rule the name is breaking A $Fred X-Y AZ sum student grade Dice_1 7G _Wisconsin U_W_Madison Muk-town A1

Variables reserved words Some words are called reserved words or keywords and CANNOT be used as variable names. Python Key Words: False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise

Assignment Statements Assignment statements are used to assign a value to the variable This is how the mailbox saves a piece of information in its location

Variables and Assignment in Python ALL variables in Python MUST be Assigned a value BEFORE it is used in your program. Assignment Statements use the = to assign values to a variable.

Assignment Statements General Format: <variable name> = <expression> Single Variable Name (the variable in which the result is stored; a.k.a. the receiving variable) Assignment Operator Expression can be: a number another variable any algebraic expression a string or string expression

Assignment VS. Equivalency In Math an = means: Both sides of the equation will have an equivalent value 6 + 3 = 5 + 4 In Python an = means: the expression value on the RIGHT side is being assigned to the value of the LEFT age = 17 The age variable has saved the value 17 in memory

Assigning Variable Values You MUST have the variable name you want to store the value in on the LEFT The value in the variable on the LEFT (receiving variable) is replaced by the new value on the RIGHT The value previously held is now gone. The values of any variables on the RIGHT side of the equals sign DO NOT CHANGE. The receiving variable should be the SAME DATA TYPE as the expression on the right.

Examples of Assignment Code age = 17 # int variable time1 = 32.23 # float variable First_Name = Sarah # string variable **NOTE: When String values are assigned their expression must be contained in quotes!** (just like in the print statement)

You Try: For each situation, decide on a variable name and write an assignment statement for the variable. 1) You are writing a program to calculate the area of a triangle and need a variable to save the length of the rectangle. 2) Your program intends to organize student data for MHS and you need a variable to save a student where a student lives. 3) Your program finds the average score on a test and needs a variable to save the number of test scores entered.

Re-Assignment Variables can (AND WILL) be reassigned multiple times throughout a program. Variable values are changed as the computer works with the program! Reusing a variable, when appropriate, makes our program more efficient

Re-Assignment You can reassign the value of the variable at ANY time REWRITES what is saved at that location Original value is ERASED and replace with the new value Ex: score = 10 # int score variable created score = 50 # score rewritten to 50 score = 20 # score rewritten to 20 **score will not remember that it was originally set to 10 or was set to 50 at some point it will only remember that it is currently set to 20**

Valid or Invalid For each statement. Decide whether the code is VALID or INVALID. If it is INVALID please state what the error is 1. num2 = 5 4. Sarah = name 2. _hello = 20*5 5. 3 + 6*2 = answer 3. 7score = 120.23

Print Statements with Variable Names Print statements can use variable names instead of quotations to output to the screen var1 = Hello var2 = Goodbye var3 = Aloha print(var1, var2, var3) Output:

Print Statements: + sign Can also use + signs to concatenate statements together NOTE: + sign does NOT add spaces like the, var1 = Hello var2 = Goodbye var3 = Aloha print(var1 + var2 + var3) Output:

Use the Variable Names Below to Write down what EXACTLY will output to the screen A = Apple B = Banana C = Carrot 1. print(a + B) 2. print(a + C, B + C) 3. print(c, B) 4. print(b + C + B)

Use the Variable Names Below to Write down what EXACTLY will output to the screen A = Apple B = Banana C = Carrot 1. print(a + B) AppleBanana 2. print(a + C, B + C) AppleCarrot BananaCarrot 3. print(c, B) Carrot Banana 4. print(b + C + B) BananaCarrotBanana

Your Task Use Quizlet Start a set of Flashcards for Computer History Things you should know: 1. All of these terms are Early Information Processing 2. All inventions know their basic purpose 3. All people know their invention as well as any special names they are known by

DAY 2 History & Binary

I can.. Identify important inventions and people in the history of computers. Understand binary numbers. Convert binary numbers to decimal numbers. Convert decimal numbers to binary numbers.

First Generation Computers (1946-1959) First generation computers were purely electronic devices Characterized by the use of vacuum tubes in circuitry to perform calculations and process information Vacuum tubes (like a light bulb) heat up a cathode, the cathode shoots electrons used like on/off switches

ENIAC Made in 1946 First electronic computer using vacuum tubes for computing memory

ENIAC fun facts Primarily used to calculate artillery firing tables What would take humans 20 hours took ENIAC 30 seconds By the end of its operation in 1956 it contained 20,000 vacuum tubes

John von Neumann (1903-1957) Created concept of the stored-program computer Program was read from punched cards in computer s memory! These programs were represented using machine language (binary) Also designed serial processor executes commands one at a time sequentially This computer design is known as the von Neumann architecture

EDVAC Develop in 1949 First American-made computer to make use of binary to represent information in a computer First US computer to use von Neumann s stored-program concept

UNIVAC I First used in 1951 First commercially produced computer Used continuously by the census bureau until 1963

Second Generation Computers (1959-1965) Characterized by the use of transistors Transistors allowed for the miniaturization necessary for the space program Computer s internal memory was made of magnetic core materials Allowed for a distinction between main (internal) and auxiliary (external) memory

Grace Hopper (1906-1992) Navy officer first assigned to programming the Mark I She created the first compiler to translate a program into machine language!

Other Important Developments Assembly Languages were developed used symbolic codes to represent binary notation of machine language The first common High Level Languages were develop to run on many different types of computers COBOL and FORTRAN Computers were smaller, faster, and more powerful AND required less energy to operate

Let s Talk about Numbering Systems!

Numbering Systems TPS Brainstorm everything you know about the following numbers systems Decimal Binary Octal Hexadecimal

Decimal Numbers Our Number System 17093

Decimal Numbers Our Number System 17093 10 10000s 1000s 100s 10s 1s 10 4 10 3 10 2 10 1 10 0 Decimal Numbers Our Number System Each number is a digit 0-9 (10 Possibilities)

Binary Number 10011 2 16s 2 4 8s 2 3 4s 2 2 Binary Number System Same Idea, just Base 2 Only Need TWO numbers 0 and 1 2s 2 1 1s 2 0

Converting Binary into decimal Remember binary is base 2! 11001101 2 128 64 32 16 8 4 2 1 Add together each power of 2 that is used to create the binary number! 128 + 62 + 8 + 4 + 1 = 205 11001101 is the same as 205 in our decimal number system

Let s Convert the following into decimal 10111010 2

Convert the following from Binary into Decimal. 1) 10111 2 5) 1101 2 2) 10001 2 6) 1001 2 3) 111 2 7) 11001 2 4) 1111 2 8) 11111111 2

Check Your Answers! 1) 10111 2 23 5) 1101 2 13 2) 10001 2 17 6) 1001 2 9 3) 111 2 7 7) 11001 2 25 4) 1111 2 15 8) 11111111 2 255

Converting Decimal into Binary! 93

Convert the following from Decimal into Binary. 1) 17 10 5) 108 10 2) 40 10 6) 96 10 3) 64 10 7) 72 10 4) 29 10 8) 9 10

Check your Answer 1) 17 10001 5) 108 1101100 2) 40 101000 6) 96 1100000 3) 64 1000000 7) 72 1001000 4) 29 11101 8) 9 1001

Your Task Use Quizlet Continue your set of Flashcards for Computer History Things you should know: 1. All of these terms are First Generation or Second Generation computers 2. Know the difference between first generation and second generation computers 3. All inventions know their basic purpose 4. All people know their invention as well as any special names they are known by

DAY 3 History and Binary, Octal, Hexadecimal

I can.. Identify important inventions and people in the history of computers. Understand binary numbers. Understand octal numbers. Understand hexadecimal numbers. Add binary numbers.

Third Generation Computers (1965-1971) Characterized by the use of Integrated Circuits Integrated circuits = hundreds of transistors on a single block or chip of silicon

The First Three Generations of Circuitry Vacuum Tubes Transistors Integrated Circuits

Other Important Developments New or improved auxiliary memory device and I/O devices were introduced ASCII Code was developed using 7 or 8 bits to represent a keyboard characters (8 bits = 1 byte) The BASIC language developed as an interpreter language (translated and run one line at a time)

Fourth Generation Computers (1971-1981) Characterized by the use of Large-Scale Integrated Circuits also known as microchips Microchips= tens of thousands (or more) transistors on a single chip Computers were becoming faster and more powerful!

Other Important Developments Apple dominated the microcomputer market Many new, powerful programming languages developed: Pascal, C, and Forth

Let s Talk about Numbering Systems! (again)

Numbering Systems Yesterday we discussed the Decimal and Binary Numbering Systems Today we will discuss the other two Decimal Binary Octal Hexadecimal

Decimal Numbers Our Number System 17093 10000s 1000s 100s 10s 1s 10 4 10 3 10 2 10 1 10 0 Decimal Numbers Our Number System Each number is a digit 0-9 (10 Possibilities)

Binary Number 10011 16s 2 4 8s 2 3 4s 2 2 Binary Number System Same Idea, just Base 2 Only Need TWO numbers 0 and 1 2s 2 1 1s 2 0

Octal Number System BASE 8 42632 8 4096s 512s 64s 8s 1s 8 4 8 3 8 2 8 1 8 0 Octal Numbers Each number is a digit 0-7 (8 possibilities)

Convert the following octal number into our decimal number system 4096 4653 512 64 8 8 1

Convert the following from octal into decimal 1) 57 8 2) 123 8 3) 7635 8 4) 65352 8

Check your answers! 1) 57 8 47 10 2) 123 8 83 10 3) 7635 8 3997 10 4) 65352 8 27370 10

Hexadecimal Number System BASE 16 42632 16 65536s 4096s 256s 16s 1s 16 4 16 3 16 2 16 1 16 0 Hexademical Numbers Each number is a digit 0-9 or letter A-F

Convert the following octal number into our decimal number system 65536 5e9a 4096 256 16 16 1 Each number is a digit 0-9 or letter A-F

Convert the following from hexademical into decimal 1) B7 16 2) 1F3 16 3) 7685 16 4) 6ABC2 16

Convert the following from hexademical into decimal 1) B7 16 183 10 2) 1F3 16 499 10 3) 7685 16 30341 10 4) 6ABC2 16 437186 10

Adding in Binary 2 Main Approaches 1. Use Binary Addition techniques 2. Change into decimal add change back into binary

Let s try it both ways 1011 + 110

Trying the Following: Perform the following 1) 11001 +1101 2) 1101 + 1011 3) 110011 + 1111 4) 11010 + 1011

Trying the Following: Perform the following 1) 11001 +1101 100110 2) 1101 + 1011 11000 3) 110011 + 1111 1000010 4) 11010 + 1011 100101

Your Task Use Quizlet Continue your set of Flashcards for Computer History Things you should know: 1. Know the difference between third generation and fourth generation computers 2. Key important inventions during each period Worksheet 2A

DAY 4 History and Math in Python!

I can.. Identify important inventions and people in the history of computers. Predict the result of mathematical calculations in Python.

Fifth Generation Computers (1983-1990) Characterized by the use of Very Large Scale Integrated Circuits Very Large Scale Integrated circuits = hundreds of thousands of transistors on a single silicon chip Processing speeds reached over a MILLION instructions per second

Sixth Generation Computers (1990-?) Characterized by the use of Very, Very Large Scale Integrated Circuits Very, Very Large Scale Integrated circuits = millions of transistors on a single silicon chip Processing speeds reached and exceed a TRILLION of operations per second

Basic Mathematics Integer: Double: +: 4 + 5 = 9 -: 9 11 = -2 *: 2*3 = 6 +: 2.1 + 3.4 = 5.5 -: 5 2.5 = 2.5 *: 4*3.3 = 13.2 Some math behaves exactly as you expect!

Basic Mathematics Integers: Floats: //: 14//4 = 3 % 14%4 = 2 (% = called modulus) /: 14/4 = 3.5 Some math does not behave as expected!!

Integer Math // = integer division - Performed only on integers - Produces an integer solution - How many times the second integer fits into the first integer % = modulus - Finds the remainder after integer division has taken place - Ex: 16 // 5 = 3 20 // 12 = 1 - Ex: 16 % 5 = 1 20 % 12 = 8

Let s try an Example Together 42 // 8 = 42 % 8 = 42 / 8 =

Try the following on your own 1) 12.5/6 = 2) 16//4 = 3) 27//5 = 4) 27%5 = 5) 25//9 = 6) 25%9 = 7) 10%4 = 8) 16.0/6 = 9) 20%7 = 10) 62//8 =

Examples: Pay close attention to int vs Double 1) 12.5/6 = 2.08333 2) 16//4 = 4 3) 27//5 = 5 4) 27%5 = 2 5) 25//9 = 2 6) 25%9 = 7 7) 10%4 = 2 8) 16.0/6 = 2.66667 9) 20%7 = 6 10) 62//8 = 7

Order of Operations Matters! P E M/D/% A/S

Try a few on your own 1) 4*(3 6)/2 + 4 2) 0.5*10*(18%4)//3 7 + 8 3) 16//4*5//8*6%7 4) 5*4 17%5 + 20.0/5*10 2*8 5) 48/(-8)*3 10-36%5*2

Try a few on your own 1) 4*(3 6)/2 + 4 2) 0.5*10*(18%4)//3 7 + 8 3) 16//4*5//8*6%7 4) 5*4 17%5 + 20.0/5*10 2*8 5) 48/(-8)*3 10-36%5*2-2 4 5 42-30

Math Function (Math Library) Similar to importing graphics we can import a variety of math functions into our Python Programs to use You must have the following at the beginning of your code: import math

Math Functions (import math) Math Code math.pow(2,3) What does it do?? math.sqrt(64) math.pi math.ceil(5.2) math.floor(5.7) int(3.7)

Math Functions (import math) Math Code math.pow(2,3) math.sqrt(64) math.pi math.ceil(5.2) math.floor(5.7) int(3.7) What does it do?? Exponent: 2 3 Square root: 64 π Round up (ceiling) 6 Round down (floor) 5 Turns into Integer 3

Give the Output for Each Statement 1. 16 + math.pow(2, 4) - 18%7 2. 19 3*( 14 // 5 ) ) 3. int(25/2) 25 // 2 4. 13 + math.sqrt(9) 15 //6 + 13 % 6 5. math.floor(9.5) + math.ceil(6.27)

Give the Output for Each Statement 1. 16 + math.pow(2, 4) - 18%7 2. 19 3 *( 14 // 5 ) 3. int(25/2) 25 // 2 4. 13 + math.sqrt(9) 15 //6 + 13 % 6 5. math.floor(9.5) + math.ceil(6.27) 28 13 0 15 16

Your Task Worksheet 2B

DAY 4 Gathering Input in Python

I can.. Gather input from the user of a program in Python.

Inputting Information Python Syntax: <variable name> = input( <prompt> ) Python will save the user s response as a STRING in the variable location

Examples Name = input( Enter your name: ) input( Hit any key to quit ) length = input( Enter the length of the rectangle: ) **What do we do when we want numerical input?**

Casting Casting converts one variable type into another! We can cast a string variable received from input by using int, float, or eval

Examples time = float(input( Enter time: )) **will turn the time into a float and save it as a float** age = int(input( How old are you?: )) **will turn the time into a float and save it as a float** weight = eval(input( Enter your weight )) **will convert to whatever data type the user inputs during the execution of the program**

Demo Program We want to create a program that will convert pounds to kilograms ALGORITHM: The steps, in order to solve a program Example algorithm: Introduce Program Input information (weight) Calculate weight in kilograms (lbs/2.2) Output answer (weight in kilograms)

Demo Program # First I/O Calculation Program # Converts pounds to kilograms print( This program converts your weight in pounds to kilograms\n\n ) # program description lbs = eval(input( Enter your weight in lbs: )) # takes in user lbs, converts to number and saves in lbs variable kgs = lbs/2.2 # converts lbs to kgs and saves in variable kgs

Demo Program (part 2) print( \n\nyour weight in kgs is:, kgs) # concatenates string displays kgs to the screen print( \n\n\n ) # adds extra spaces input( Hit any key to quit ) # allows user to end program

In Class Task Write a program that will do the following: Take is 5 test scores from the user Calculate the average test score Output the average test score to the user **When you are done, show Ms. Wardecke and she will give you Worksheet 2C to work on for homework**