Presentation is loading. Please wait.

Presentation is loading. Please wait.

INPUT & VARIABLES.

Similar presentations


Presentation on theme: "INPUT & VARIABLES."— Presentation transcript:

1 INPUT & VARIABLES

2 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables Input - 1. The process of entering information into a computer. 2. The information entered or put into a computer for processing. CMPF 144-Fundamental of Computing Theory (Python)

3 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables This is what you type in your PC : print "Halt!" s = raw_input("Who Goes there? ") print "You may pass,", s variable CMPF 144-Fundamental of Computing Theory (Python)

4 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables When you ran the program, here is what your screen showed: OUTPUT : Halt! Who Goes there? Josh You may pass, Josh input CMPF 144-Fundamental of Computing Theory (Python)

5 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables When you ran the program you probably noticed how you had to type in your name and then press Enter. Then the program printed out some more text and also your name. This is an example of input. CMPF 144-Fundamental of Computing Theory (Python)

6 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables Variables are like a box that can store some piece of data. Next is a program to show examples of variables: Josh CMPF 144-Fundamental of Computing Theory (Python)

7 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables SAMPLE PROGRAM 1 a = 123.4 b23 = ’Spam’ first_name = "Bill" b = 432 c = a + b print "a + b is", c print "first_name is", first_name print "Sorted Parts, After Midnight or",b23 OUTPUT : a + b is 555.4 first_name is Bill Sorted Parts, After Midnight or Spam CMPF 144-Fundamental of Computing Theory (Python)

8 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables 123.4 Spam a b23 Bill 432 c first_name b CMPF 144-Fundamental of Computing Theory (Python)

9 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables Strings are a sequence of letters, numbers and other characters. Example : Spam , Bill ,a + b is ,first_name is - string 123.4 , number CMPF 144-Fundamental of Computing Theory (Python)

10 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables SAMPLE PROGRAM 2 a = 1 print a a = a + 1 a = a * 2 OUTPUT : 1 2 4 CMPF 144-Fundamental of Computing Theory (Python)

11 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables a 2 X 2 = 4 1 a a 1 + 1 = 2 print a print a + 1 print a X 2 CMPF 144-Fundamental of Computing Theory (Python)

12 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables SAMPLE PROGRAM 3 num = input("Type in a Number: ") str = raw_input("Type in a string:") print "num =", num print "num * 2 =",num*2 print "str =", str print "str * 2 =",str*2 OUTPUT : Type in a Number: 12.34 Type in a String: Hello num = 12.34 num * 2 = 24.68 str = Hello str * 2 = HelloHello CMPF 144-Fundamental of Computing Theory (Python)

13 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables num variable gets data from input str variable gets data from raw_input If you want the user to type in a number use input because it returns a number If you want the user to type in a string use raw_input because it returns a string CMPF 144-Fundamental of Computing Theory (Python)

14 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables Numbers are of type int or float (which are short for ’integer’ and ’floating point’ respectively) Strings are of type string Integers and floats can be worked on by mathematical functions, strings cannot. CMPF 144-Fundamental of Computing Theory (Python)

15 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables SAMPLE PROGRAM 4 #This programs calculates rate and distance problems print "Input a rate and a distance" rate = input("Rate:") distance = input("Distance:") print "Time:",distance/rate OUTPUT : Input a rate and a distance Rate:5 Distance:10 Time: 2 CMPF 144-Fundamental of Computing Theory (Python)

16 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables 5 rate distance 10 10 / 5 = 2 time = distance/rate CMPF 144-Fundamental of Computing Theory (Python)

17 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables SAMPLE PROGRAM 4 (with error) #This programs calculates rate and distance problems print "Input a rate and a distance" rate = raw_input("Rate:") distance = input("Distance:") print "Time:",distance/rate string could not worked on by mathematical functions Note : raw_input uses to get data in string type CMPF 144-Fundamental of Computing Theory (Python)

18 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables SAMPLE PROGRAM 5 #This program calculates the perimeter and area of a rectangle print "Calculate information about a rectangle" length = input("Length:") width = input("Width:") print "Area",length*width print "Perimeter",2*length+2*width OUTPUT : > python area.py Calculate information about a rectangle Length:4 Width:3 Area 12 Perimeter 14 CMPF 144-Fundamental of Computing Theory (Python)

19 CMPF 144-Fundamental of Computing Theory (Python)
Input & Variables SAMPLE PROGRAM 6 #Converts Fahrenheit to Celsius temp = input("Farenheit temperature:") print (temp-32.0)*5.0/9.0 OUTPUT : > python temperature.py Farenheit temperature:32 0.0 Farenheit temperature:-40 -40.0 Farenheit temperature:212 100.0 CMPF 144-Fundamental of Computing Theory (Python)


Download ppt "INPUT & VARIABLES."

Similar presentations


Ads by Google