class: center, middle .center[] # Python Course: Data Types Shahid Beheshti University Instructor: S. M. Masoud Sadrnezhaad --- # Sequences - Strings: `'Life is short, you need Python.'` - Lists: `[1, "jane", 2.9, None, [3.14, 2.71]]` - Tuples: `(1, 4, "john")` - Sequences are iterable. --- # Showtime! Let's see 'indexing and slicing sequences' in action. --- # Sequence Operators - Operators - `+`: concat - `*`: repetition - `in`: membership - `not in`: non-membership - Functions - `len()` - `max()`, `min()`, `sum()` - `sorted()` --- # Sequence Operators (Contd) ```python >>> L = [1, 2, 3, 4, 5] >>> 2 in L True >>> 16 not in L True >>> 'ali' in L False ``` --- # List Comprehension - A list comprehension is a syntactic construct for creating a list based on existing lists. .center[] --- # Showtime! Let's see 'list comprehension' in action. --- # String Formatting - % operator - `.format` method ```python >>> 'Number of %s is %d.' % ('steps', 10) Number of steps is 10. >>> 'Number of {0} is {1}.'.format('steps', 10) Number of steps is 10. ``` --- # Tuples Tuples are immutable lists. ```python >>> a = (1, 2) >>> a[0] = 5 Traceback (most recent call last): File "
", line 1, in
TypeError: 'tuple' object does not support item assignment ``` --- # Sets - A set is an unordered “bag” of unique values. - A single set can contain values of any immutable datatype (can contain strings & tuples, but not lists). - Once you have two sets, you can do standard set operations like union, intersection, and set difference. --- # Dictionaries - A dictionary is an unordered set of key-value pairs. - Keys must be immutable. - Values can be anything. --- # Showtime! Let's see 'sets & dictionaries' in action. --- # References - Dr. Hamid Zarrabi-Zadeh's Web Course, Fall 2013. - http://www.diveintopython3.net/ - http://openbookproject.net/thinkcs/python/english3e/ - http://en.wikipedia.org/wiki/Python_(programming_language) --- class: center, middle .center[] # Thank you. Any questions?