Working with Data in Python PDF
Document Details
Tags
Related
- Ch-2 Python Introduction PDF
- Fundamentals of Programming - ITM200 Data Variables and Calculations PDF
- Python Basics PDF
- ITP 313 Python Fundamentals and Data Types PDF
- BSc (Hons) Electronics Ist Sem - Programming Fundamentals using Python PDF
- Cours Python - Programmations: Concepts Fondamentaux - Pratique (PDF)
Summary
This document provides a basic introduction to working with data in Python, focusing on data types (strings, integers, floats, booleans) and operators. It explains how Python handles different data types in computations. It also covers the essential features of data type conversion for proper code execution in Python to perform correct mathematical operations involving mixed data types.
Full Transcript
Working with Data Data Types and Operators (in Python) Outline Recall Cont. of Variables Data Types Operators in Python Recall Output statement: print(“Hello world”) Assignment statement: x = 2 Input statement: name = input(“Enter name: ”) Data Types…? Before we get into data ty...
Working with Data Data Types and Operators (in Python) Outline Recall Cont. of Variables Data Types Operators in Python Recall Output statement: print(“Hello world”) Assignment statement: x = 2 Input statement: name = input(“Enter name: ”) Data Types…? Before we get into data types, let’s look at the following code num1 = input("Enter number1: ") num2 = input("Enter number2: ") _sum = num1 + num2 print("Sum is", _sum) Please code, run, and observe the execution What happened? ??? 1 + 1 should equal 2 But what if we were adding texts together? "Hello" + " " + "World" "Hello World" So what happened was we added the text value of 1 ("1") "1" + "1" = "11" This what’s happening behind the scene Data Types…? Our issue here is that we have an improper data type In certain cases, you’d have to tell Python what type of data a specific value is We need to tell Python to convert the type of our input to a number Data Types…? Try making the following changes: num1 = input("Enter number1: ") num2 = input("Enter number2: ") _sum = int(num1) + int(num2) print("Sum is", _sum) Please code, run, and observe the execution Data Types…? This can also be written like… num1 = int(input("Enter number1: ")) num2 = int(input("Enter number2: ")) _sum = num1 + num2 print("Sum is %d" % (_sum)) Data Types…? Or like this… num1 = input("Enter number1: ") num2 = input("Enter number2: ") num1 = int(num1) num2 = int(num2) _sum = num1 + num2 print("Sum is %d" % (_sum)) So let’s go over the data types we need to know Strings Refers to text data Can be composed of a string of letters, numerals (figures that denote a number), and other special characters Example: the name of a game you bought during a Steam sale When writing Strings literally into code, they are enclosed in quotation marks x = "The quick brown fox jumped over the lazy dog" Integers Refer to whole numbers They are usually used to denote counts of objects Example: the number of games you bought during the Steam sale When written into code, simply write the numerals that denote the value of the desired whole number No decimal point x = 4 Floats Floating point numbers refer to decimal numbers They are usually used to denote measures or data that requires precision Example: how much money did you spend during the Steam sale When written into code, write the numerals that denote the value of the desired decimal number with the decimal point x = 50.5 ; x = 1.0 Boolean Refers to data that can only either be true or false Unlike Strings or numbers with infinite possible values These are often used to denote whether a state is or is not Example: having enough money to buy all the games you want during the Steam sale When written into code, boolean values are written as True or False Note the capitalization! What data type would best represent the following data? 1. Age 1. Integer 2. License plate 2. String 3. Fahrenheit 3. Float 4. Page number 4. Integer 5. Validity of driver’s license 5. Boolean Data Types Computers need to know some data’s type in order to know how to manipulate or treat the value Should all real-life categories have only data types? Not all the time! Your ID number can be either a string or an integer Your age can be either an integer or a float…. or a string! You just need to follow proper syntax and are in control of the design and flow of logic of your solution Combining Everything Together… string_variable = "This is a string" integer_variable = 1000 float_variable = 0.78 print (string_variable, integer_variable, float_variable) For Python, it just needs to see the proper format and it’ll know what data type to assign Data Type Converters Recall our previous num1 = input("Enter number1: ") problem… num2 = input("Enter number2: ") input(), by int(var) takes default, returns a num1 = int(num1) the value inside num2 = int(num2) some variable string and converts it We solved this _sum = num1 + num2 to an integer problem using int() print("Sum is %d" % (_sum)) Data Type Converters We know that Python knows what data type a value has if its in the proper format 4.0 is float "hello" is a string But again, input() returns a string value all the time Hence, we use data type converts! Data Type Converters Try them out in your shell and see what outputs All the data types have their own commands for converting data Integers int("4"); int(4.9) Floats float(1); float("4") Strings str(1) Booleans bool("4"); bool(0) Some concepts may be a little too technical, but executing code and observing what happens goes a long way Any questions so far? So… From the adding number problem, we’ve already used the + or the addition operator What are the other operators in Python? Operators in Python Operators in Python There are many Operator Symbol operators in Python, Negation - but the one’s listed in Addition + the table are what Subtraction - you need to know Multiplication * Division / Integer Division (Floor Division) // Modulo (Remainder) % Operators in Python Most concepts Operator Symbol should be familiar, Negation - but you’ll just need to Addition + get use to how Subtraction - Python 3 works with Multiplication * numbers Division / Integer Division (Floor Division) // Modulo (Remainder) % Operators in Python Performing +, -, and * Operator Symbol between integers and Negation - floats results in a Addition + float Subtraction - In the shell, try out: Multiplication * 4.0 * 5 Division / Output: Integer Division (Floor Division) // 20.0 Modulo (Remainder) % Operators in Python Division (/) produces Operator Symbol a float, regardless of Negation - the operands Addition + In the shell, try out: Subtraction - 4 / 5 Multiplication * Output: Division / Integer Division (Floor Division) // 0.8 Modulo (Remainder) % Operators in Python Integer Division (//) Operator Symbol works like regular Negation - division, but floors Addition + the value Subtraction - In the shell, try out: Multiplication * 9 // 5 Division / Output: Integer Division (Floor Division) // 1 Modulo (Remainder) % Operators in Python Modulo (%) works Operator Symbol like regular division, Negation - but you get the Addition + remainder Subtraction - In the shell, try out: Multiplication * 9 % 5 Division / Output: Integer Division (Floor Division) // 4 Modulo (Remainder) % Operators in Python Modulo (%) works Operator Symbol like regular division, Negation - but you get the Addition + remainder Subtraction - In the shell, try out: Multiplication * 21 % 3 Division / Output: Integer Division (Floor Division) // 0 Modulo (Remainder) % Manually computing for the following and then run them in Python to see if you got them correct a) 3 + -4 a) 7 b) 2 * 4.0 b) 8.0 c) 5 / 2 c) 2.5 d) 10 // 3 d) 3 e) 16 % 3 e) 1 If there is time remaining… Try creating a Python script for the following: Get an input Fahrenheit, convert to it Celsius, and display the conversion (32°F − 32) × 5/9 = 0°C Get 2 input representing 2 angles of a triangle, compute for the third angle, and display the output a + b + c = 180 Summary We discussed Data types! Integer, float, string, boolean Operators! +, -, *, /, //, % Thanks and see you all next meeting!