Changing one data type into another

Slides:



Advertisements
Similar presentations
For(int i = 1; i
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.
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 Primitive Types Operators Basic input and output.
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
When you write to a file, what happens to the content that’s already there? A.The new content replaces the original content. B.The new content goes at.
Input, Output and Variables GCSE Computer Science – Python.
Fundamentals of Programming I Higher-Order Functions
Python Basics.
Numbers and arithmetic
Lesson 03: Variables and Types
Numbers and Arithmetic
Introduction to programming in java
Century High-School Tutoring Program
Writing & reading txt files with python 3
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Creates the file on disk and opens it for writing
Introduction to Python
Intro to CS Nov 2, 2015.
CSC 131: Introduction to Computer Science
Presented By S.Yamuna AP/IT
ITM 352 Expressions, Precedence, Working with Strings Class #5
Register Use Policy Conventions
Basic notes on pointers in C
What are variables? Using input()
Learning to Program in Python
Multiplication and integers
Python I/O.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Learning Outcomes –Lesson 4
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Creates the file on disk and opens it for writing
COMPUTER PROGRAMMING PYTHON
Fundamentals of Data Structures
G7 programing language Teacher / Shamsa Hassan Alhassouni.
Margaret Derrington KCL Easter 2014
单击 此处添加标题.
Feedback Pace Different ideas for delivery Debugging strategies
Programming Training Main Points: - Working with strings.
Lesson 03: Variables and Types
GCSE Computing Lesson 6.
Python Lessons 9 & 10 Mr. Husch.
What are variables? Using input()
Starter answer these questions in your book
Variables In today’s lesson we will look at: what a variable is
Nate Brunelle Today: Strings, Type Casting
Python Basics with Jupyter Notebook
Appending or adding to a file using python
Median Statement problems.
Click to add your text.
Multiple Choice Quiz.
What are variables? Using input()
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.
CSE 231 Lab 5.
Python 4 and 5 Mr. Husch.
Starter Activities GCSE Python.
Nate Brunelle Today: Strings, Type Casting
Python fundamental.
IST256 : Applications Programming for Information Systems
Python Creating a calculator.
Presentation transcript:

Changing one data type into another CASTING Changing one data type into another

Types of data in python 3 number = 3 number_as_a_string = “67” You can print the type of data using print(type(number)) Try a text string or other data type of you choice Data convert to a list requires the list function

Convert from a string into a list of integers numbers_as_string = ['4', '6', '7', '12', '49', '56'] numbers_as_string = list(map(int, numbers_as_string)) Data convert to a list requires the list function

Convert from string to integer numbers_as_string = ['4', '6', '7', '12', '49', '56'] print (type(numbers_as_string)) for i in numbers_as_string: print (type(i)) numbers_as_string = list(map(int, numbers_as_string)) How it works Replace the int with the data type, for example, float

Casting of data read from a file string to a list part 1 numbers_as_string = [] print(numbers_as_string) Create a new text file called data_file and add some numbers to it Open the text file and convert the values to a new list Reads the line of data

Casting of data read from a file string to a list part 2 numbers_as_string = [] print(numbers_as_string) file_obj = open('data_file' + ".txt") #opens file numbers_as_string = file_obj.readline() #reads file contents Open the text file and convert the values to a new list Reads the line of data

Casting of data read from a file string to a list part 3 numbers_as_string = [] print(numbers_as_string) file_obj = open('data_file' + ".txt")#opens file numbers_as_string = file_obj.readline()#reads file contents print (numbers_as_string) # print values print (type(numbers_as_string)) #print type Open the text file and convert the values to a new list Reads the line of data

Casting of data read from a file string to a list part 4 numbers_as_string = [] print(numbers_as_string) file_obj = open('data_file' + ".txt")#opens file numbers_as_string = file_obj.readline()#reads file contents print (numbers_as_string) # print values print (type(numbers_as_string)) #print type newlist = list(numbers_as_string) #convert to a list print (newlist) print (type(newlist)) Open the text file and convert the values to a new list Reads the line of data

TRY YOUR OWN Change the data in the data_file Add some text, what happens? Make a new data file, add some data, open it and convert it Create a file which converts integers into strings Add you own twist

QUIZ – TRY THESE What type of data is this, “Hi how are you today?” What data type is this, int, Give an example What data type is this, Up / Down Give another example What data type is correct for this number 3.14 Which of these is the odd one out and why, int, string, float, Name one other data type.