3a - Operators, Expression and Data Types (1).pptx

Full Transcript

Computational Thinking with Programming Lecture - 3 Operators, Expression and Data types @csebennett @cse_bennett Today’s Outline Previous Session: Int...

Computational Thinking with Programming Lecture - 3 Operators, Expression and Data types @csebennett @cse_bennett Today’s Outline Previous Session: Introduction to Token. Statements and Expressions. Today’s Session: Data Types, indexing and Operators Mutable and Immutable data types. Data type conversion. Operators. Hands on Session with Jupyter Notebook: We will practice on the Python basic elements in Jupyter Notebook. 2 Data Types Data type is the classification of the type of values that can be assigned to variables. Dynamically typed languages, the interpreter itself predicts the data type of the Python Variable based on the type of value assigned to that variable. >>> x = 2  x is a variable and 2 is its value >>> type(x) int  x can be assigned different values; >>> x = 2.3 hence, its type changes accordingly >>> type(x) float 3 Data Types (Numbers) The number data type is divided into the following five data types: >>> a = >>> a = 2 0x19 >>> type(a) >>> type(a) Integer int int Long Integer (removed from >>> py3)a = 2.5 >>> a = 2 + Floating-point Numbers >>> type(a) 5j float >>>type(a) Complex Numbers >>> a = complex 0o11 >>> type(a) >>> type(a) float int >>> a = 9999999L 5 Data Types (String) Python string is an ordered collection of characters which is used to represent and store the text-based information. Strings are stored as individual characters in a contiguous memory location. It can be accessed from both directions: forward and backward. >>> a = “Bennett” >>> print(a) Bennett >>> a = ‘University’ >>> print(a) University 6 Data Types (String) Characters of string can be individually accessed using a method called indexing. Characters can be accessed from both directions: forward and backward. Forward indexing starts form 0, 1, 2…. Whereas, backward indexing starts form −1, −2, −3… >>> a = “Bennett University” >>> print(a) t >>> print(a[-1]) y >>> print(a[-5]) r 7 Mutable Data Types: Data types in python where the value assigned to a variable can be changed Immutable Data Types: Data types in python where the value assigned to a variable cannot be changed Lists Tuples Data Types (Tuples) Tuple data type in Python is a collection of various immutable Python objects separated by commas. Tuples are generally used for different Python Data Types. A Python tuple is created using parentheses around the elements in the tuple. Although using parentheses is only optional, it is considered >>> a =a(1,2,3,4) good practice to use them. >>> print(a) (1,2,3,4) >>> a = (‘ABC’,’DEF’,’XYZ’) >>>print(a) 11 Data Types (Tuple) To access an element of a tuple, we simply use the index of that element. We use square brackets. Reverse Indexing by using indexes as −1, −2, −3, and so on, where −1 represents the last element. Slicing that is, extract some elements from the tuple. >>> a = (1,2,3,4) >>> a = (1,2,3,4) >>> print(a) >>> print(a[-1]) 2 4 >>> a = >>> a = (‘ABC’,’DEF’,’XYZ’) (‘ABC’,’DEF’,’XYZ’) >>>print(a) >>>print(a[1:]) XYZ (DEF, XYZ) 12 Data Types (List) Unlike strings, lists can contain any sort of objects: numbers, strings, and even other lists. Python lists are: Ordered collections of arbitrary objects >>> a = Accessed by offset [2,3,4,5] Arrays of object references >>> b = Of variable length, heterogeneous, and arbitrarily [“Bennett”, nestable “University”] Of the category, mutable sequence >>> print(a,b) Data types in which elements are stored in the index >>> [2,3,4,5] basis with starting index as 0 Enclosed between square brackets ‘[]’ [‘Bennett’,’Uni versity’] 13 Data Types (List) Much similar to strings, we can use the index number to access items in lists as shown below. Accessing a List Using Reverse Indexing To access a list in reverse order, we have to use indexing from −1, −2…. Here, −1 represents the last item in the list. >>> a = [“Bennett”, “University”, “Computer”] >>> print(a) Bennett >>> print(a[-1]) Computer >>> print(a) 14 Data Types (Set) It is an unordered collection of elements which means that a set is a collection that stores elements of different Python Data Types. In Python sets, elements don’t have a specific order. Sets in Python can’t have duplicates. Each item is unique. The elements>>> ofmyset a set =in{“bennett”, Python are immutable. They “computer”, can’t accept changes once added. “science”} >>>print(myset) {'bennett', 'computer', 'science’} >>>myset = set((“bennett”, “computer”, “science”)) 15 Data Types ( Dictionary) Yet another unordered collection of elements. Difference between dictionary and other unordered Python data types such as sets lies in the fact that unlike sets, a dictionary contains keys and values rather than just elements. Unlike lists the values in dictionaries are accessed using keys and not by their positions >>>dict1 ={“Branch”:”computer”,”College”:”Bennett”,”year”:20 20} >>>print (dict1) {‘Branch’:’computer’,’College’:’Bennett’,’year’:2020} 16 Data Types ( Dictionary) The keys are separated from their respective values by a colon (:) between them, and each key–value pair is separated using commas (,). All items are enclosed in curly braces. While the values in dictionaries may repeat, the keys are always unique. The value can be of any data type, but the keys should be of immutable data type, that is Using the key inside square brackets like we used to use >>>dict1 the index inside square brackets. ={“Branch”:”computer”,”College”:”Bennett”,”year”:20 20} >>>print (dict1[year]) 17 Datatype Conversion As a matter of fact, we can do various kinds of conversions between strings, integers and floats using the built-in int, float, and str functions >>> x = >>> y = >>> z = 10 "20" 30.0 >>> >>> >>> float(x) float(y) int(z) 10.0 20.0 30 >>> >>> >>> str(x) int(y) str(z) integer '10'  float string 20  float float  integer '30.0' integer >>>  string string >>> integer float >>>  string 18 Explicit and Implicit Data Type Conversion Data conversion can happen in two ways in Python 1. Explicit Data Conversion (we saw this earlier with the int, float, and str built-in functions) 2. Implicit Data Conversion Takes place automatically during run time between ONLY numeric values E.g., Adding a float and an integer will automatically result in a float value E.g., Adding a string and an integer (or a float) will result in an error since string is not numeric Applies type promotion to avoid loss of information Conversion goes from integer to float (e.g., upon adding a float and an integer) and not vice versa so as the fractional part of the float is not lost 19 Implicit Data Type Conversion: Examples  The result of an expression that involves >>> print(2 + 3.4) 5.4 a float number alongside (an) integer >>> print( 2 + 3) number(s) is a float number 5 >>> print(9/5 * 27 + 32) 80.6 >>> print(9//5 * 27 + 32) 59 >>> print(5.9 + 4.2) 10.100000000000001 20 Implicit Data Type Conversion: Examples  The result of an expression that involves >>> print(2 + 3.4) 5.4 a float number alongside (an) integer >>> print( 2 + 3) number(s) is a float number 5  The result of an expression that involves >>> print(9/5 * 27 + 32) values of the same data type will not result 80.6 in any conversion >>> print(9//5 * 27 + 32) 59 >>> print(5.9 + 4.2) 10.100000000000001 21 Operators Arithmetic Operators (**, *,/,//,%,+,_) Comparison Operators (=, ==) Python Assignment Operators (=, +=, -=, *=, /=) Logical Operators (and, or, not) Bitwise Operators (&, |, ~, >>, = Greater Than variables Equals to But do not change the variables >>a=7>7 >>>a=7>7 or Outpu X Y and 2>-1 2>-1 X Y t t >>>print(a) >>>print(a) TRU FALSE TRUE TRU TRUE TRUE >>>print(7 and 0 or 5) TRUE TRUE E E 5 FALS FALS TRUE FALSE TRUE TRUE E E TRU TRU FALSE FALSE FALSE TRUE E E FALS FALS FALSE FALSE FALSE FALSE E E 32 Logical operators Just the reverse NOT of what is there. not(true)  false 33 Bitwise operators Operator Description Bitwise operators act on & Binary AND Operator copies a bit to the result if it exists in both operands as if they were operands string of binary digits. | Binary OR It copies a bit if it exists in either operand. It operates bit by bit. ^ Binary XOR It copies the bit if it is set in one operand but not both. 2 is 10 in binary and 7 is 111. ~ Binary Ones It is unary and has the effect of Complement 'flipping' bits. In the table below: > Binary Right The left operands value is Shift moved right by the number of bits specified by the right 35 operand. Bitwise operators AND Operati Outp Operati Outp Same as 101 & 111. on ut on ut This results in 101 0&0 0 0|0 0 5&7 Which is binary for 5. 0&1 1&0 0 0 0|1 1|0 1 1 1&1 1 1|1 1 Binary for 4 is 0100, and that for 8 is 1000. After operation, output is 1100 4|8 Which is binary for 12. Bitwise operators are used in encryption 37 algorithms and applications Bitwise operators XOR XOR Operator Output (exclusive Otherwise 0^0 0 OR) returns 1 , it returns 0^1 1 If one operand 0. 1^0 1 is 0 and 1^1 0 another is 1. 38 Shift operator A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left Shift operator Left Shift () 11|00 52 40 Bitwise operators Operators Let’s Solve () 10*4>>2 and 3 ** +x, -x, ~x *, /, //, % +, - 10%(15=, 10/(5-5) =,

Use Quizgecko on...
Browser
Browser