Data Types and Conversions Input from the Keyboard
![Data Types and Conversions, Input from the Keyboard CS 303 E: Elements of Computers Data Types and Conversions, Input from the Keyboard CS 303 E: Elements of Computers](https://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-1.jpg)
Data Types and Conversions, Input from the Keyboard CS 303 E: Elements of Computers and Programming
![What is a Data Type? The type of value stored by a variable n What is a Data Type? The type of value stored by a variable n](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-2.jpg)
What is a Data Type? The type of value stored by a variable n Two so far: n – String: represents text n For now, only use for input/output n More later this semester – Numerical n Breaks down into many other types n Types have different internal representations
![Numerical Data Types n int: whole numbers – Stored in 4 bytes (32 bits) Numerical Data Types n int: whole numbers – Stored in 4 bytes (32 bits)](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-3.jpg)
Numerical Data Types n int: whole numbers – Stored in 4 bytes (32 bits) – Can represent whole numbers -2^31 though 2^31 -1 – Computations are exact n float: decimal point numbers – Large range, but fixed precision – Not exact (remember, all data is 0 s and 1 s!) n n Example: . 1+. 2 =. 3000000004 long: whole numbers larger or smaller than int – Specify by adding “L” to the end
![Data Types: Differences n Integers should be your default – Integer arithmetic is faster Data Types: Differences n Integers should be your default – Integer arithmetic is faster](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-4.jpg)
Data Types: Differences n Integers should be your default – Integer arithmetic is faster and more precise n Most arithmetic operators behave as you would expect for all data types – Except integer division (gozinta)---result is an integer n Example: result = 5/2 n longs are integers, so they behave this way too – float division behaves as you expect: n Example: result = 5. 0/2. 0
![Data Types: Automatic Conversion n int to float: – int op float = float Data Types: Automatic Conversion n int to float: – int op float = float](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-5.jpg)
Data Types: Automatic Conversion n int to float: – int op float = float – Examples: 5. 0/2 16 -(3/2) 16 -(3. 0/2) But what about: 3. 5 + 6/4? n int to long: – Large integers are converted to long Example: 5^31
![Data Types: Examples of Automatic Conversion 6. 0+2 7 -. 25 3/2 3. 0/2 Data Types: Examples of Automatic Conversion 6. 0+2 7 -. 25 3/2 3. 0/2](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-6.jpg)
Data Types: Examples of Automatic Conversion 6. 0+2 7 -. 25 3/2 3. 0/2 5**31 5. 0**31 5 L+3 5. 0/2+3 3. 0 + 5/2 4/5 – 2 4. 0/5 – 2 4/5 – 2. 0
![Data Types: Explicit Conversion n Python provides functions to do this: float(<put number here>) Data Types: Explicit Conversion n Python provides functions to do this: float(<put number here>)](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-7.jpg)
Data Types: Explicit Conversion n Python provides functions to do this: float(<put number here>) int(<put number here>) #truncates! long(<put number here>) n If you would rather round to the nearest whole number use round() – Also takes the number as the argument
![Data Types: Examples of Explicit Conversion What is the output? float(3) int(3. 9) int(101. Data Types: Examples of Explicit Conversion What is the output? float(3) int(3. 9) int(101.](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-8.jpg)
Data Types: Examples of Explicit Conversion What is the output? float(3) int(3. 9) int(101. 566) long(3. 9) IDLE: Average of three test scores.
![i. Clicker Question: Data Types What is the output of the following code? result i. Clicker Question: Data Types What is the output of the following code? result](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-9.jpg)
i. Clicker Question: Data Types What is the output of the following code? result = 5. 0 + 11/2 print result A. 10. 5 B. 10. 0 C. 8 D. 8. 0 E. 10
![Keyboard Input n n Read data from the user during program execution Two ways: Keyboard Input n n Read data from the user during program execution Two ways:](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-10.jpg)
Keyboard Input n n Read data from the user during program execution Two ways: – input(): reads numbers – raw_input(): reads strings n How they work: – wait for the user to enter a value – read what has been typed when the user hits the Enter or Return key
![Keyboard Input: input() input(<put prompt string here>) n n Prompts the user to enter Keyboard Input: input() input(<put prompt string here>) n n Prompts the user to enter](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-11.jpg)
Keyboard Input: input() input(<put prompt string here>) n n Prompts the user to enter a number Assigns entered number to a variable Example: score 1 = input(“Please enter the first exam score: ”) IDLE: Modify average program to prompt the user
![Keboard Input: raw_input() raw_input(<put prompt here>) n n Prompts the user to enter a Keboard Input: raw_input() raw_input(<put prompt here>) n n Prompts the user to enter a](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-12.jpg)
Keboard Input: raw_input() raw_input(<put prompt here>) n n Prompts the user to enter a string Assigns entered string to a variable Example: name = raw_input(“What’s your full name? ”) print name What happens if you give a number to raw_input()? What happens if you give a string to input()?
![i. Clicker Question: Keyboard Input Which data type does input() expect? A. string B. i. Clicker Question: Keyboard Input Which data type does input() expect? A. string B.](http://slidetodoc.com/presentation_image_h2/932578f2f3fb58462efd0a900660a3d8/image-13.jpg)
i. Clicker Question: Keyboard Input Which data type does input() expect? A. string B. int C. float D. int, float, or long
- Slides: 13