Module 2: Data Types, Formatting and Errors

¡Supera tus tareas y exámenes ahora con Quizwiz!

How to Write a Code

1. Determine Requirements 2. Write the Source Code (Input) 3. Convert Source Code to Object Code (this is what Python does) 4. Run the Program 5. Check the Output (the written source code)

Type Function

Accepts one argument (a value to analyze) and returns the data type of the item in question. type("hi") >> python will print out "str" type(10) >> python will print out "int" type(4.5) >> python will print out "float" They have different sets of operations and the data type limits us on what you can actually do with the piece of data you are working with.

Format Function

Can be used to create customized/printable versions of numbers and strings. >> x = 1021021.2342345 To simplify this, you can use the format function. >> formatted_x = format(x, ".2f") = 1021021.23 >> In the "" you declare the data type and how you'd like to format it. ".2f" = round x to 2 decimal places and x is a float. Once you use the format function it always returns a STRING. >> It is no longer something used in a math function unless you convert it after formatting. Including commas in x: >> formatted_x = format(x, ",.2f") >> print(formatted_x) >> 1,021,021.23. When working with an integer you use "d" as it is a decimal integer. >> x=1000 >> formatted_x = format(x, "d") >> print(formatted_x) >> '1000'

Advanced Math Operations

Division operations: floating point division and integer division. >> FPD: print(5/2) = 2.5 >> ID: print(5//2) = 2 >> Throws away the remaining portion and rounds down. Exponent Operation: ** >> For square root you can do: print(2**0.5) Module Operator: %, show you the remaining value when using //. >> What is leftover. >> Print(5/2) = 2.5, Print(5//2) = 2, Print(5%2) = 1 (the remainder)

Nesting

Executing inner most functions first and then the outer most function. Which means you can do data conversions all in one line. >> var1 = float(input("Enter a number: "))

Mixed Type Expressions

Expression that includes multiple data types (ints and floats). Answer = 5+2 >> Print (answer, type(answer)) = 7, int. Answer = 5.2 + 2.7 >> Print (answer, type(answer)) = 7.9, float. Answer = "Hi" >> Print (answer, type(answer)) = Hi, str. Will be evaluated as a float if you have any floats in your expression so that the decimal values will remain in your answer.

Basic String Operations

How to manipulate a string. >> a = "apple" >> b = "pear" >> c = a + b. You can use the "+" operator with a String and an integer to cause Python to combine the two Strings into a single Compound String. >> c = a + b = applepear. If c = 5, you cannot do a + b + c. Cannot add a float or int into the mix. >> New_word = a * 3 You can use the "*" operation with a String and an integer to cause Python to "repeat" the String that many times. >> New_word = appleappleapple.

Aligning Format Tool

If you want to make things aligned with one another you can use formatting to put them into columns. >> Word1 = "Name" >> Word2 = "School" >> Word3 = "Nikki" >> Word 4 = "Music Business" >> Fword1 = format(word1, "8s") , 8 meaning 8 characters wide. >> Fword3 = format(word3, "8s") >> Fword2 = format(word2, "20s") >> Fword4 = format(word4, "20s") >> Print(fword1, fword2) >> Print(fowrd3, fword 4) Name School Nikki Music Business ** Spaces include characters in the word as well. If you want to make something align to the right you can do ... >> Fword1 = format(word1, ">8s") >> Fword3 = format(word3, ">8s") >> Fword2 = format(word2, ">20s") >> Fword4 = format(word4, ">20s")

Commenting / # / """

Renders the statements afterwards inactive. A commenting tool. Can be side notes if you don't want them to be included when you run your program. >> Indicates to Python that we are typing a text that they should ignore. Can be used as a way to document your code.

Data Types

Three fundamental data types. Strings, Integers, Floating Point Value. >> String: var1 = "Bird" >> Int: var2 = 5 >> Floats: var3 = 4.9 >> Type: type(var1) = str

Errors

Three typical kinds of errors" #1: Syntax Error >> Misuse of the Python language. >> name = "Craig >> name = "Craig", print("Hello there", name When you run ^^ answer will be highlighted in red to tell you there is an error on the line above. #2: Runtime Error >> When your program crashes while running your program. >> x = 5, y = 0 >> print(x/y). >> Will cause a crash as you cannot divide anything by zero. #3: Logic Error When your program runs and does not crash, however, it does not perform the way you are expecting it to. The code is miswritten as you have not used the correct steps to program your code.

Data Type Conversions

x = 1.99, y = "2.99" results to data type mismatch. Thus one of the data types must be converted into another. Convert y into a float. >> Float_version_of_y = float(y) Float Function: Accepts one argument and converts it into a floating point value. >> Answer = x + float_version_of_y = 4.98. a = "1.99", b = 2.99, c = "5" >> New_a = float(a) = 1.99 >> New_b = str(b) = "2.99" >> New_c = int(c) = 5 At times data conversions will not be possible. E.g. d = "apple", new_d = float(d). ** This becomes important when you are asking the user for inputs. Inputs always come out as a strings. So when you receive a number from the user, you have to convert the string into a numeric data type, float/int.


Conjuntos de estudio relacionados

Operating Systems Chapter 1 and 2

View Set

Fathers of physics and equations

View Set

Abnormal Psychology - Anxiety Disorders

View Set

Ch. 18 - Community as Client: Assessment and Analysis

View Set

Basic Livestock Surgical Procedures

View Set

Ch 22: Endocrine Clinical Assessment and Diagnostic Procedures jk

View Set