Fundamentals of Programming - ITM200 Data Variables and Calculations PDF
Document Details
Uploaded by Deleted User
Zeinab Noorian
Tags
Summary
This document provides an introduction to data variables and calculations in Python. It covers topics such as arithmetic operators, data types (integers, floats, strings), and demonstrates how to perform calculations using variables. The document also includes examples and explanations, as well as potential errors.
Full Transcript
Fundamentals of Programming – ITM200 Data Variables and Calculations Zeinab Noorian © Copyright Youcef Derbal, 2022-2023 Agenda Perform calculations using the usual arithmetic operators Output messages and calculation results Get and interpret us...
Fundamentals of Programming – ITM200 Data Variables and Calculations Zeinab Noorian © Copyright Youcef Derbal, 2022-2023 Agenda Perform calculations using the usual arithmetic operators Output messages and calculation results Get and interpret user input Perform calculations/processing using user input © Copyright Youcef Derbal, 2022-2023 Calculations ❑ Calculations make up one of the most important functions provided through computer programming. 9* Celsius fahrenheit = + 32 5 ❑ The formula involves the following elements: 1. The variables fahrenheit and celsius to hold the temperatures 2. The multiply, divide and add arithmetic operators 3. The Assignment operator “=“ which assigns the Right- Hand-Side (RHS) of the formula to its Left-Hand-Side (LHS). © Copyright Youcef Derbal, 2022-2023 Calculations 9* Celsius fahrenheit = + 32 5 ❑ Coding this formula using Python will entail the use of constructs equivalent to the three mathematical elements making up the formula: Variables Arithmetic Operators Assignment Operator © Copyright Youcef Derbal, 2022-2023 Conversion of Celsius to Fahrenheit Arithmetic expression using the arithmetic Assignment Operator ( = ) operators: Assigns the RHS to the LHS Multiplication (*) Division (/) Addition (+) © Copyright Youcef Derbal, 2022-2023 Conversion of Celsius to Fahrenheit What is the inside story? variables such as celsius and fahrenheit need to be stored in RAM for the program to perform its calculations (conversion in this case) hence they are categorized in relation to their memory needs We decided (for our program) to represent the temperature in Celsius using integers such as : -2, -1,0,1,..,10,..,50,51,.... Variables that are to hold integers will be categorized as int variables, while those holding real numbers (decimal point) are said to be float variables © Copyright Youcef Derbal, 2022-2023 Data Types ❑The Type categorizes variables based on the nature of the data they hold. int – used for variables that hold natural numbers 0,1,2,3,… and their negative counterparts -1,-2,-3… float –real numbers such as -23.5, -9, 0, 0.5, 1, 2.34,6,.. str – alphanumeric character strings such as “ITM200”, “123ER” © Copyright Youcef Derbal, 2022-2023 Data Types ❑ The use of the Data Type concept allows the enforcement of desirable constraints on the operations that are allowed to be applied on the data. ❑Ex: We should be able to write program instructions that add variables that hold int values “12” and “14” - it makes sense (numbers can be added), but we should not be able to write instructions to add variables holding strings of characters! © Copyright Youcef Derbal, 2022-2023 Data Variables In Python, variable names may be any alphanumeric string where the first character must not be a numeral. and finally pass as for raise assert from return Variable names are break global try class if while case sensitive and continue import with may not be any of def in yield del is False Python’s listed elif lambda True reserved keywords. else non-local None except not Exec Or © Copyright Youcef Derbal, 2022-2023 Arithmetic Operators Operator Operation * Multiplication / Division - Subtraction ** Exponentiation % Modulus (remainder of a division between integers // Floor division ( integer part of a division result) + addition ❑ Arithmetic operators used in Python work in the usual way ❑ The evaluation of an arithmetic expression is done from left to right in the following order (rules of precedence) : 1. Evaluate whatever is enclosed between brackets “( )” 2. Evaluate ** 3. Evaluate *, /, %,// 4. Evaluate +, - © Copyright Youcef Derbal, 2022-2023 Arithmetic Operations Shorthand Statement Shorthand x=x+y x += y x=x–y x -= y x=x*y x *= y x=x/y x /= y x = x // y x //= y x=x%y x %= y x = x ** y x **= y © Copyright Youcef Derbal, 2022-2023 Dynamically Typed Dynamically Typed: Variables can be bound (by assignment) to different types at execution time. © Copyright Youcef Derbal, 2022-2023 Strongly Typed Strongly Typed: Operations that are incompatible with the type are not allowed, and will result in run-time errors © Copyright Youcef Derbal, 2022-2023 Characters in Python No character data type – a character is a string of a single character ord() and chr() functions can be used to get the ASCII/Unicode value of a character and vis-a-versa, respectively. © Copyright Youcef Derbal, 2022-2023 Formatting Outputs str(n) converts n to a str type to allow concatenation to another string using the +, interpreted in this case as a string concatenation operator round(x,n) rounds x to n digits after the decimal point © Copyright Youcef Derbal, 2022-2023 More on round() https://docs.python.org/3/library/functions.html#round © Copyright Youcef Derbal, 2022-2023 Types & Operations © Copyright Youcef Derbal, 2022-2023 Strings It is a string of characters s = “ITM200” Every character has a position in the string indicated by an index. index 0 1 2 3 4 5 I T M 2 0 0 Length = 6 s = “” # Empty string s = None # None string © Copyright Youcef Derbal, 2022-2023 String Manipulation © Copyright Youcef Derbal, 2022-2023 User Input © Copyright Youcef Derbal, 2022-2023 Processing User Input Although the inputs are numbers, the second last instruction of the program is a string concatenation rather than an addition. In order for the addition to be possible, the contents of s1 and s2 need to be converted to numbers before they are added. © Copyright Youcef Derbal, 2022-2023 Calculation using Input The Python built-in function int() converts a string of numerals into a number as illustrated by the example, above. © Copyright Youcef Derbal, 2022-2023 Data Type Conversion ❑ int – integers ❑ float – real numbers ❑ str – string of characters ❑ bool – Boolean variable that can only take the values True or False The following Python built-in functions can be used, when possible, to do type conversion: int(), float(), str() and bool() © Copyright Youcef Derbal, 2022-2023 Parsing a str into an int - Error The “conversion” of a string of numerals to an int will succeed only if the string can be interpreted as an integer. The example given below will result in a run-time error. s = “7A” j = int(s) A similar error would result if the string looks like a number but not an int. For example, although “7.5” looks like a number (float) however, 7.5 it is not an int and hence would cause a run-time error as well. © Copyright Youcef Derbal, 2022-2023 Parsing a str into a float - Error The “conversion” of a string of numerals to a float will succeed only if the string can be interpreted as a float. The example below will result in a run-time error. s = “7A” j = float(s) If the string can be interpreted as an int no error will result because int values do fit in float variables. Ex: 7 is an int and can also be taken as a float. © Copyright Youcef Derbal, 2022-2023 Type Conversion © Copyright Youcef Derbal, 2022-2023 String Methods © Copyright Youcef Derbal, 2022-2023