Some Basic Python Data Types Int Float Complex

Some Basic Python Data Types Int Float Complex Str List Tuple Dict 2345 999 2+3 2. 345 2 E 12 5/2 2 + 4 j 1+j (2 + 2 j)*(2 -2 j) “ 2345” “ 23. 45” ‘ 123’ //note 1 [1, 3, 5, 8] [“ 123”, “ 456” ] [12, 4. 56, “zzz”] (1, 3, 5, 8) 1, 3, 5, 8 (“a”, 2, “b, 3) {“man”: “male human”, “woman”: ”female human”, 1: “one”} Exercise: get used to python. Do for first example of each row” >>>2345 >>>a=2345 >>>print(a) Be free. E. g. insert space here and there, no match brackets, … , Note 1: The single quote used by Power Point is a Unicode character. Python does not handle it well. If you copy from Power Point and paste to python, you will get an error.

Variable - name which refers to value >>>A=1 The value 1 has a name ‘A’ >>>B=1 The value 1 has a name ‘B’ Symbol table Symb Mmry Address Content Name A B Police. Station 1 Main St Town. Hall 2 Main St Tam. Nest 10 Main st • • • FF 00 4320 FF 00 4328 1 2 Address Content Policemen Town Office Tams Why ‘variable’? Letters, digits, ‘_’. But must not begin with digit. Case sensitive Must not be a reserved word (see text, p. 21 for list of reserved words) Type Use meaningful names – “rate”, “amount”, not “zz”, “x 1”, “x 2”, etc

Variables Exercise For each of the following Python statement, state what is in memory after execution of the statement: >>>a=1 a: b: >>>b=2 a: b: >>>a=a + b a: b: >>>b=a-b a: b: >>>a=a-b a: b: ------------------------------->>> x = 1 x: y: temp: >>> y= 2 x: y: temp: >>> temp =x x: y: temp: >>> x= y x: y: temp: >>> y= temp x: y: temp:

Int and Float – Operations 1 >>> type(1234) >>>1234. 0 >>>type(234. 0) >> 1234 + 5678 >>> type (1234 + 5678) >>> 123 + 1 >>> 123* 2 >>>120 /2 >>>3 + 2 * 2 >>>3*6 >>>4**2 Use variables >>>num 1 = 5 >>>float 1 = 3. 2 >>>num = num + 2 >>>num=5; num += 2 >>>num=5; num*= 2; num >>>num=5; num/2; num; type(num) >>> num=12 >>>fnum=3. 0 >>>ans=num + fnum >>>ans >>>type(ans) # what happens?

Int and Float – Operations 2 Try out +, -, *, **, / with floats and integers Take your time and intentionally be free (e. g. extra spaces; ident a space before typing “a=123” Inject a tab; etc. Use parenthesis Use multiple operations such as 2*(3+2) Any surprise?

Int and Float – Operation 3 “Floor” operation: >>> 7/2 >>> 7//2 >> 7. 1//2 # floor >> Int(7. 1//2) >>7. 2//2. 1 Remainder operation: >>> 13%3 #ans is 1 >> 13. 0%3 #ans is 1. 0

A Problem With floats >>>7. 1 %2 >>2. 55 – 2. 5 >>5. 1 /2 – 2. 5 What’s the problem? See Dale/Lewis, pp. 67 -68 Use round function: Round (2. 55 – 2. 5, 2) # 2 places after decimal

A=A+1 How can A equals A + 1 Example 1: A=1; A=A+1 Example 2 • A=1 • B=2 • A= A + B • Print(A, B) • • • Example 2 A=1, B=2 A=B B=A Print (A, B)

String s 1=‘my first string’ s 2=“my second string” s 3=s 1 + s 2 # s 3=‘my first stringmy second string’ s 4=“ “ s 3=s 1+s 4+s 2 # s 3=‘my first string my second string’ s 5=“n” #n refers to ASCII character 10 (LF) Dale/Lewis p. 70 s 6=s 1+s 5+s 2 # >>>print(s 6) - two lines will be displayed

Boolean Type This type h 2 values: True, False Try the following: >>> 2> 1 >>> type (True) >>> type(true) >>> 1 > 2 >>> type(False) >>> type(false) >>> a = 1 >>> b = 2 >>> b==a >>>type(b==a) >>>b!=a >>>type(b!=a)

Assignments (1) A=1, B=2, How do you swap A and B using third temporary variable? Bonus question: How do you do it WITHOUT a temporary variable? As programmer, which method should you use? Why? (2) Steps to convert 123 to binary using // operator and % operator (3) Steps to convert decimal 54321 to hexadecimal using // operator and % operator (4) Convert hex 555 to decimal using ** (5) Severance, Ch 2. 15 ex 2 -5. (Put answer in 1 docx file or txt file. Email to instructor. )
- Slides: 11