Python Data Types

¡Supera tus tareas y exámenes ahora con Quizwiz!

Conversion between data types ... continued

>>> set([1,2,3]) {1, 2, 3} >>> tuple({5,6,7}) (5, 6, 7) >>> list('hello') ['h', 'e', 'l', 'l', 'o'] To convert to dictionary, each element must be a pair >>> dict([[1,2],[3,4]]) {1: 2, 3: 4} >>> dict([(3,26),(4,44)]) {3: 26, 4: 44}

Numbers

Integers, floating point numbers and complex numbers falls under Python numbers category. They are defined as int, float and complex class in Python. We can use the type() function to know which class a variable or a value belongs to and the isinstance() function to check if an object belongs to a particular class.

Lists are mutable

Lists are mutable, meaning, value of elements of a list can be altered. >>> a = [1,2,3] >>> a[2]=4 >>> a [1, 2, 4]

Dictionary { }

Unordered collection of key-value pairs. It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value. In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type. We use key to retrieve the respective value. But not the other way around. d = {1:'dog', 'cat':2} print(type(d)) #output dict print("d[1] = ", d[1]); #output = d[1] = 'dog' print ("d['cat'] = " , d['cat']); #output d[2] = 'cat' print("d[2] = " , d[2]) # Generates error

Tuple ( ) - immutable

an ordered sequence of items same as list.The only difference is that tuples are immutable. Tuples once created cannot be modified. Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically. It is defined within parentheses () where items are separated by commas. >>> t = (5,'program', 1+3j) We can use the slicing operator [] to extract items but we cannot change its value.

List - [ ]

an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type. Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ]. >>> a = [1, 2.2, 'python'] We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python. a = [5,10,15,20,25,30,35,40] # a[2] = 15 print("a[2] = ", a[2]) # a[0:3] = [5, 10, 15] print("a[0:3] = ", a[0:3])

String

sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ''' or""" Like list and tuple, slicing operator [ ] can be used with string. Strings are immutable.

Set { }

Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered. a = {5,2,3,1,4} # printing set variable print("a = ", a) #output a = {1, 2, 3, 4, 5} # data type of variable a print(type(a)) #output <class 'set'>

Conversion between data types

We can convert between different data types by using different type conversion functions like int(), float(), str() etc. float (5) #output 5.0 Conversion from float to int will truncate the value (make it closer to zero). Conversion to and from string (str) must contain compatible values.

Set { } ... continued

We can perform set operations like union, intersection on two sets. Set have unique values. They eliminate duplicates. b = {1,2,3,2,2,1,3,4,} print(b) #output {1, 2, 3, 4}


Conjuntos de estudio relacionados

Mastering Astronomy, Astronomy 2, Chapter 2

View Set

Brain and Behavior Exam 1: Chapters 1-3

View Set

Chapter 4: Genes and Their Evolution

View Set

Titanic-Voices from the Disaster Boat Vocabulary

View Set

Chapter 44: Assessment and Management of Patients with Biliary

View Set

Skeletal system A & P test review

View Set