Type Casting. Type casting Sometimes you need a piece of data converted to a different type In Python you do a typecast to cause that to happen int(3.599)

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Getting Input in Python Assumes assignment statement, typecasts.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Types and Arithmetic Operators
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Modules and Objects Introduction to Computing Science and Programming I.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
Python November 14, Unit 7. Python Hello world, in class.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Input, Output, and Processing
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Computer Science 101 Introduction to Programming.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Mathematical Calculations in Java Mrs. C. Furman.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
THE DOUBLE VARIABLE The double variable can hold very large (or small) numbers. The maximum and minimum values are 17 followed by 307 zero’s.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Introduction to Computing Science and Programming I
Numbers and arithmetic
Agenda Introduction Computer Programs Python Variables Assignment
Numbers and Arithmetic
Whatcha doin'? Aims: To start using Python. To understand loops.
Software Development I/O and Numbers
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Introduction to Python
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
2.5 Another Java Application: Adding Integers
Variables, Expressions, and IO
Lists in Python Parallel lists.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Exceptions and files Taken from notes by Dr. Neil Moore
Math in C The math blocks you've used in Scratch can all be recreated in C!
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Exceptions and files Taken from notes by Dr. Neil Moore
Arithmetic Expressions & Data Conversions
Passing Parameters by value
Feedback Pace Different ideas for delivery Debugging strategies
Variables, Data Types & Math
Variables, Data Types & Math
Topics Introduction to File Input and Output
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.
Data Types and Maths Programming Guides.
Class code for pythonroom.com cchsp2cs
More Basics of Python Common types of data we will work with
Arithmetic Expressions & Data Conversions
Python Creating a calculator.
Presentation transcript:

Type Casting

Type casting Sometimes you need a piece of data converted to a different type In Python you do a typecast to cause that to happen int(3.599) will give 3 – note that it throws away the fraction, does not round it float (4349) will give – it looks the same but remember that it is stored differently in memory float(“23.4”) will give 23.4, but float(“abc”) will give a runtime error x = 5.3 y = int(x) makes y have the value 5, does NOT change x’s value

Type cast for strings str will turn a number into a string. This is handy when you are using functions which require a string as an argument str(3.5) will give “3.5” str(“abc”) will give “abc” – this one is silly but Python allows it. If the data is already a string, why make it a string again?

Typecasting with input function Typecasting is used pretty often with the input function The input function always returns a string If you need the data in the string interpreted as a different type, you have to use a typecast to change it to that type my_num = int(input(“Enter a number “)) This is the shorthand way of writing two statements my_data = input(“Enter a number “)) my_num = int(my_data)

Strongly typed versus weakly typed Python allows one variable to hold many different types of values, one after the other This means the language is weakly typed Languages like C++ and Java are strongly typed. You have to state what type a variable is and it does not change its type for the whole program Types are good to help you avoid errors that involve using the wrong type of data or using the data in an illogical way In Python it is smarter to keep each variable to one type. If you need to use it as a different type, typecast it and put it into a different variable