accy570lab_week2

html

School

University of Illinois, Urbana Champaign *

*We aren’t endorsed by this school

Course

570

Subject

Computer Science

Date

Dec 6, 2023

Type

html

Pages

7

Uploaded by HighnessLoris3604

Report
Week 2 Lab Variables Data types Operators Loops Python Idendifiers (variable, function, class names) Contains only letters, numbers and underscores. The first character must be a letter or an underscore character. Python variables traditionally start with a lowercase letter. Cannot be one of the reserved Python keywords, listed in the code block below. In [1]: help('keywords') Here is a list of the Python keywords. Enter any keyword to get more help. False break for not None class from or True continue global pass __peg_parser__ def if raise and del import return as elif in try assert else is while async except lambda with await finally nonlocal yield Variables A Python variable is a name for a value. It's a python identifier . The variable naming convention is lowercase with words separated by underscores as necessary to improve readability. Some people prefer mixedCase over snake_case . You can name it whatever you want as long as you follow python identifier naming rules. We can create a new variable by assigning a value to it using = . In following code, we define a variable class_name and print out the value of the variable. In [3]: class_name = "ACCY570 Lab" print(class_name) ACCY570 Lab In [4]: section='a' print(section) print(class_name) a ACCY570 Lab In [6]: #class = "ACCY570 Lab" Exercise 1 Mark invalid variable names with "x" at the end of the name. For example, if you think accy570 is an invalid variable name, make it accy570 x . You may edit the cell by double clicking the cell to enter edit mode. accy570 x _accy570 x
__accy570 x __ x ACCY_570 x accy-570 accy$570 570lab if Primitive Data Types int float str bool True False check data type To check the data type of a variable, we can use type() function: type(dt) convert to another primitive type int() float() str() In [4]: name = 'Linden Lu' weight = 70 height = 1.75 In [5]: type(name) Out[5]: str In [7]: name2 = "D'Angelo" name2 Out[7]: "D'Angelo" In [8]: name3 = 'D\'Angelo' name3 Out[8]: "D'Angelo" In [8]: type(weight) Out[8]: int In [9]: type(height) Out[9]: float In [10]: type(1.0) Out[10]: float
In [11]: height > 1.8 Out[11]: False In [12]: type(height>1.8) Out[12]: bool In [13]: float(weight) Out[13]: 70.0 In [14]: height Out[14]: 1.75 In [15]: int(height) Out[15]: 1 In [16]: str(height) Out[16]: '1.75' In [17]: float('3.14') Out[17]: 3.14 Python Operators mathematical operators. The following list presents the basic mathematical operators, in order of precedence (operators in the same table cell have the same precedence): Operator Description Example () Parentheses for grouping (2 + 3) ** Exponential function 2**3 * Multiplication 2*3.1 / Division 3.2 / 1.2 // Integer Division 5//2 % Remainder 5%2 + Addition 1.45 + 3.14 - Subtraction 5.3 - 2.125
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
Operator Description Example Comparison Operators Operator Description Example == Equivalence operator a == b != Not equivalence operator a != b > Greater than operator a > b >= Greater than or equal to operator a >= b < Less than operator a < b <= Less than or equal to operator a < b Logical Operators Operator Description Example or or operator, True if either condition is true a or b and and operator, True only if both conditions are true a and b not not operator, opposite of condition not a In [18]: #calcuate area of a circle r = 10 area = 3.14 * r**2 area Out[18]: 314.0 In [13]: seconds_in_a_day = 60*60*24 seconds_in_a_day Out[13]: 86400 Exercise 2 Calcuate number of seconds in a week and assign the value to variable seconds_in_a_week , then display the value. In [17]: seconds_in_a_week=60*60*24*7 seconds_in_a_week Out[17]: 604800
Exercise 3 In the code cell below, define two variables weight and height with some values(ie. your own weight in kg and height in meter). Then calculate body mass index and assign the result to variable bmi . Then display bmi. \begin{equation} \textrm{BMI} = \frac{\textrm{Weight(in kg)}}{\textrm{Height(in m)}^2} \end{equation} In [36]: weight=46 height=1.61 bmi=weight/(height**2) bmi Out[36]: 17.746228926353147 Loop while loop while some-condition: Do something Some action to change condition In [20]: # find multiples of 17 under 100 num = 0 while num < 100: if num%17 == 0: print(num) num += 1 #num = num + 1 0 17 34 51 68 85 for loop for var in iterable: Do someting In [21]: mylist = [0,1,2,3,4,5,6,7,8,9] total = 0 for i in mylist: total += i print(total) 45 Exercise 4 Print extreme Nasdaq returns. Loop through qqq_returns, which is the Nasdap yearly return from 2000-2021, print out the return if it is: greater than 10%(0.1) or less than -10%(-0.1)
In [37]: qqq_returns = [-0.37, -0.33, -0.38, 0.49, 0.1, 0.015, 0.07, 0.19, -0.42, 0.53, 0.19, 0.03, 0.17, 0.34, 0.18, 0.08, 0.06, 0.32, -0.01, 0.38, 0.48, 0.22] print(qqq_returns) [-0.37, -0.33, -0.38, 0.49, 0.1, 0.015, 0.07, 0.19, -0.42, 0.53, 0.19, 0.03, 0.17, 0.34, 0.18, 0.08, 0.06, 0.32, -0.01, 0.38, 0.48, 0.22] In [38]: # Your code here for i in qqq_returns: if (i>0.1) or (i<-0.1): print(i) -0.37 -0.33 -0.38 0.49 0.19 -0.42 0.53 0.19 0.17 0.34 0.18 0.32 0.38 0.48 0.22 range() In [24]: range(10) Out[24]: range(0, 10) In [25]: list(range(10)) Out[25]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [26]: for i in range(10): print(i) 0 1 2 3 4 5 6 7 8 9 In [27]: for i in range(5): print('I love Python')
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
I love Python I love Python I love Python I love Python I love Python Exercise 5 Use range() function to create a for loop and print out all numbers between 0 to 100 and are multiple of 7. Hint: use remainder to check multiple of 7 ( i % 7 == 0 ) In [43]: for i in range(100): if (i%7==0): print(i) 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98