list.sort() vs sorted(list) # Find largest and smallest in a sequence.
sum() # To find the total
len()
Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
There are various data types in Python. Some of the important types are listed below.
Lists are intended to be sequences of related data. Ordered and changeable, allows duplicate entries.
List are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0. (See the official python.org list docs.)
colors = ['red', 'blue', 'green'] print colors[0] ## red print colors[2] ## green print len(colors) ## 3Assignment with an = on lists does not make a copy. Instead, assignment makes the two variables point to the one list in memory.
b = colors ## Does not copy the listThe "empty list" is just an empty pair of brackets [ ]. The '+' works to append two lists, so [1, 2] + [3, 4] yields [1, 2, 3, 4] (this is just like + with strings).
Python provides another type that is an ordered collection of objects, called a tuple. They are diverse data structures. Ordered and unchangeable and allows duplicate items.
>>> t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')>>> t('foo', 'bar', 'baz', 'qux', 'quux', 'corge')>>> t[0]'foo'>>> t[-1]'corge'>>> t[1::2]('bar', 'qux', 'corge')A Set is a list of unique items. Unordered and unindexed, with no duplicate members
A set can be used to remove duplicate entries from a list, or ensure data does not get duplicated. If a record already exists in a set, it doesn't change.
>>> x = set(['foo', 'bar', 'baz', 'foo', 'qux'])>>> x{'qux', 'foo', 'bar', 'baz'}>>> x = set(('foo', 'bar', 'baz', 'foo', 'qux'))>>> x{'qux', 'foo', 'bar', 'baz'}A list of unique 'keys' pointing to records for fast access. Unordered, changeable and indexed. No duplicate members
The contents of a dict can be written as a series of key : value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}.
Looking up or setting a value in a dict uses square brackets, e.g. dict['foo'] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use "in" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case).
dict = {} dict['a'] = 'alpha' dict['g'] = 'gamma' dict['o'] = 'omega' print dict ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}Using Tuple: A Tuple is a comma separated sequence of items. It is created with or without (). Tuples are immutable. See this for details of tuple and list.
# A Python program to to return multiple # values from a method using tuple # This function returns a tuple def fun(): str = "you are awesome" x = 20 return str, x; # Return tuple, we could also # write (str, x) # Driver code to test above method str, x = fun() # Assign returned tuple print(str) print(x) Output:
you are awesome20https://www.geeksforgeeks.org/g-fact-41-multiple-return-values-in-python/
The term originated as an abstraction of the sequence: single, double, triple, quadruple, quintuple, sextuple, septuple, octuple, ..., n‑tuple, ..., where the prefixes are taken from the Latinnames of the numerals. The unique 0‑tuple is called the null tuple. A 1‑tuple is called a singleton, a 2‑tuple is called an ordered pair or couple, and a 3‑tuple is called a triple or triplet. The number n can be any nonnegative integer. For example, a complex number can be represented as a 2‑tuple of reals, a quaternion can be represented as a 4‑tuple, an octonion can be represented as an 8‑tuple, and a sedenion can be represented as a 16‑tuple.
Although these uses treat ‑tuple as the suffix, the original suffix was ‑ple as in "triple" (three-fold) or "decuple" (ten‑fold). This originates from medieval Latin plus (meaning "more") related to Greek ‑πλοῦς, which replaced the classical and late antique ‑plex (meaning "folded"), as in "duplex".[6][a]
So, what happens when you lock a Python programmer in a secret vault containing 1.5 TBytes of C++ source code and no internet connection? Find out as I describe how I used Python as a secret weapon of "discovery" in an epic legal battle.