Notes_on_numbers_and_assignments - Jupyter Notebook

pdf

School

University of Wisconsin, Madison *

*We aren’t endorsed by this school

Course

320

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

5

Uploaded by MegaFlagGoldfinch46

Report
10/14/22, 4:52 PM Notes_on_numbers_and_assignments - Jupyter Notebook localhost:8888/notebooks/Downloads/Notes_on_numbers_and_assignments.ipynb 1/5 Python Fundamentals: Numbers In this workbook we study two numerical data types available in Python: floats and integers. We will use these datatypes quite extensively in this class for all our quantitative exercises. In sum: Integers are whole numbers. For example: 1, 2, 3, 89, -101 are all integers. Floats are numbers with decimal points. For example 1.0, 6.2, -101.0 and 67.2542736 are all floats. Arithmetic operations on numbers A Jupyter notebook can be used as a scientific calculator: In [1]: In [2]: In [3]: In [4]: In [5]: In [6]: In [7]: Out[1]: 2 Out[2]: -4 Out[3]: 21 Out[4]: 2.6666666666666665 Out[5]: 2 Out[6]: 1 Out[7]: 9 # Addition 1 + 1 # Subtraction 3 - 7 # Multiplication 3 * 7 # Division 8 / 3 # Floor division 7 // 3 # Remainder of the floor division (modulo operator) 7 % 3 # Powers 3 ** 2
10/14/22, 4:52 PM Notes_on_numbers_and_assignments - Jupyter Notebook localhost:8888/notebooks/Downloads/Notes_on_numbers_and_assignments.ipynb 2/5 In [8]: In [1]: Priority of operations Python respect the usual priority of operations: In [9]: In the above calculation, Python first multiplied and then added . To ask Python to add the numbers before multiplying them, we can use parenthesis: In [10]: Here Python calculates and first, and then multiplies . Determining the type of a number If you want to determine the type of a number, you may use the "type" function: In [11]: In [12]: In [13]: In [14]: Out[8]: 3.0 Out[1]: 0.3333333333333333 Out[9]: 19 Out[10]: 54 Out[11]: int Out[12]: 2 Out[13]: int Out[14]: 0.6666666666666666 # Fractional powers 9 ** ( 1 / 2 ) # Negative power 3 ** ( - 1 ) 3 + 6 * 2 + 4 ( 3 + 6 ) * ( 2 + 4 ) type ( 1 ) # The "type" function returns the data type of a number, here an in 1 + 1 type ( 1 + 1 ) # The "type" function here is applied to the sum of 1 and 1, agai 2 / 3
10/14/22, 4:52 PM Notes_on_numbers_and_assignments - Jupyter Notebook localhost:8888/notebooks/Downloads/Notes_on_numbers_and_assignments.ipynb 3/5 In [15]: Variable assignment We sometime want to store values in the memory of our computer so that we can use it later. A "variable" is a location in the memory of our computer where we can store information. Variables are given name so that we can referenced then in a computer program. The operation of storing information in a variable is called an "assignment". Let's look at an example. Storing the value 3 in a variable that we label "x" can be done as follow: In [16]: Now, when we refer to "x" in our code, Python will treat it as if it were the number 3. For example: In [17]: In [18]: In [19]: In [20]: The value of a variable is not set in stone. It can be altered later on in the program. Changing the value of a variable is called a "reassignment": In [21]: In [22]: In [23]: In [24]: Out[15]: float Out[17]: 4 Out[18]: 9 Out[19]: 6 Out[20]: 27 Out[22]: 7 Out[24]: 17 type ( 2 / 3 ) x = 3 # We assign 3 to the variable x 1 + x x + x + x 2 * x x ** x x = 7 # The value of x is reassigned to 7 x x = x + x + 3 # We can use the value of x itself to reassign x x
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
10/14/22, 4:52 PM Notes_on_numbers_and_assignments - Jupyter Notebook localhost:8888/notebooks/Downloads/Notes_on_numbers_and_assignments.ipynb 4/5 Variable naming rules There are a few rules you MUST follow regarding variable names in Python. 1. Variable names cannot contain spaces. The usual practice is to u se an underscore "_" instead of a space. 2. Variable names cannot start with a number. 3. Variable names cannot start with any of these characters :'".< >/?|\()!@#$%^&*~-+ 4. Variable names cannot be one of the special keywords in Python (Python will let you know if a given variable name is a protected keyword). Moreover, there are conventions regarding the variable names that you SHOULD follow when possible 1. It is considered best practice to use lowercase names for variab les. 2. Use variable names that are evocative of the information you sto re in them To illustrate this last point, consider the following example: In [25]: In [26]: Reading the code in the above two cells, it is really easy to figure out what is being done: The cell calculate the future value of an initial investment of $1,000 when the interest rate is 5%. Now compare this to the following code: In [27]: In [28]: Both calculations yields the same output. But the second example is quite opaque. When writing code, aim for clarity for the sake of your colleagues who may have to read it. And also for the sake of your future self who may have to maintain that code. Out[26]: 1050.0 Out[28]: 1050.0 present_value = 1000 interest_rate = 0.05 future_value = ( 1 + interest_rate) * present_value future_value # Display result YiOyIoIlOvEPOtaTOes = 1000 WITHketchupITSevenBETTER = 0.05 iWAShungryWRITINGtHoSeLines = ( 1 + WITHketchupITSevenBETTER) * YiOyIoIlOvEPOtaTOe iWAShungryWRITINGtHoSeLines
10/14/22, 4:52 PM Notes_on_numbers_and_assignments - Jupyter Notebook localhost:8888/notebooks/Downloads/Notes_on_numbers_and_assignments.ipynb 5/5