Basic Syntax
Basic data structure: sequences
There are 6 built-in types of sequences. Here we introduce two of the most common - tuples and lists.
- tuple() - a function to list a sequence of values.
- Note: in Python, indexes start at 0, and the maximum is non-inclusive; for example, specifying the range 1 to 5 will include the second, third, fourth, and fifth values.
tuple((3, 1, -2)) # use the tuple function
Output: (3, 1, -2)
(3, 1, -2) # use parentheses to create a tuple
Output: (3, 1, -2)
3, 1, -2 # use a comma to separate components
Output: (3, 1, -2)
3, 1, "hello" # tuple can be used to store numbers and strings together
Output: (3, 1, "hello")
"math", "statistics", "physics", "chemistry" # a tuple of strings
Output: ('math', 'statistics', 'physics', 'chemistry')
tuple("hello") # a tuple of 5 components h, e, l, l, o
Output: ('h', 'e', 'l', 'l', 'o')
a = tuple("hello") # assigning a name to the tuple
a[1] # the second component in the tuple will be returned
Output: 'e'
a[0] # it returns the first component in the tuple
Output: 'h'
a[0:3] # it returns a tuple of the first, second,
and thrid components in the original tuple
Output: ('h', 'e', 'l')
a[2:4] # it returns a tuple of the third and fourth components in the original tuple
Output: ('l', 'l')
Note: You should notice that the index or position of a tuple starts from zero, not one.
Tutorials for learning Python tuples can be found at python.org (data structures) or tutorialspoint.
- list() - a function to list a sequence of values. Here is an example:
list((1, 2, 3)) # use the list function
Output: [1, 2, 3]
[1, 2, 3] # simply use brackets to create a list
Output: [1, 2, 3]
list("abc") # use the list function
Output: ['a', 'b', 'c']
list(("abc", 2, 3))
Output: ['abc', 2, 3]
b = [3, 1, -2, 7, -5] # assign a name to the list
b[2] # it returns the third element in the list
Output: -2
b[0] # it returns the first element in the list
Output: 3
b[1:4] # this returns a list of the second, third, and fourth elements
in the original list
Output: [1, -2, 7]
Note: The index of a list also starts from zero.
Tutorials for learning Python lists can be found at python.org (data structures) or tutorialspoint.
Difference between tuples and lists: Tuples have no methods, which means that you can't add, remove, or find an element in a tuple directly; their contents are fixed and cannot be changed without overwriting the tuple entirely.
Numeric Types - float(), int()
- float() - convert a number or a string to a floating-point number, if possible.
- int() - convert a number or a string to an integer (i.e. a whole number), if possible.
Here is an example for float() and int().
float(312)
Output: 312.0
float("312.17635")
Output: 312.17635
round(312.17635, 2) # use round() to round a number for 2 decimal places
Output: 312.18
int(312.17635) # truncate the number and replace it with the resulting integer
Output: 312
int("312") # convert a string to an integer
Output: 312
int(float("312.17635")) # convert a string to an integer
Output: 312
round(312.17635, 0) # round a number to the nearest integer
Output: 312.0
Stop: In the above example, you can find the outcomes of the last four commands are all 312. What's the difference between them? Why do we need different ways to find the same outcome? Try int("312.17635"), what do you get?
Some commonly used built-in functions in data analysis
There are a number of functions built into the Python interpreter. We introduce some commonly used built-in functions here. More details about the built-in functions can be found on this page on the official website of Python Programming Language.
Simple calculation functions: abs(), max(), min(), sum(), len()
- abs() - a function to evaluate the absolute value of a number. Try abs(-5) and see what you get.
- max() - a function to evaluate the maximum of a list of values
- min() - a function to evaluate the minimum of a list of values
- sum() - a function to evaluate the sum of a list of values
- len() - a function to evaluate the length of an object
Here is an example for abs(), max(),min(), sum(), and len().
abs(7)
Output: 7
abs(-7)
Output: 7
max(3, 1, 5, -2) # maximum of values
Output: 5
max((3, 1, 5, -2)) # maximum of values in a tuple
Output: 5
max([3, 1, 5, -2]) # maximum of values in a list
Output: 5
min(3, 1, 5, -2) # minimum of values
Output: -2
min((3, 1, 5, -2)) # miniimum of values in a tuple
Output: -2
min([3, 1, 5, -2]) # minimum of values in a list
Output: -2
sum((3, 1, 5, -2)) # sum of values in a tuple
Output: 7
sum([3, 1, 5, -2]) # sum of values in a list
Output: 7
c = 3, 1, 5, -2;
len(c) # it returns the length of a tuple
Output: 4
d = [3, 1, 5, -2];
len(d) # it returns the length of a list
Output: 4
More functions
- range() - a function to list a range of values. It indexes in the same way as a tuple or list (see above).
range(0, 10) # it returns a list of values from 0 to 9
Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(13, 19) # it returns a list of values from 13 to 18
Output: [13, 14, 15, 16, 17, 18]
- all() - a function to check if all elements of an object that is capable of returning its members one at a time are true
- any() - a function to check if any element of an object that capable of returning its members one at a time is true
Here is an example for all() and any().
all(x > 5 for x in range(0, 10))
Output: False
all([x > 5 for x in range (0, 10)])
Output: False
all([x > 5 for x in range (5, 10)])
Output: False
any(x > 5 for x in range (0, 10))
Output: True
any([x > 5 for x in range (0, 10)])
Output: True
any([x > 5 for x in range (0, 6)])
Output: False
Note: In the above example, all(x > 5 for x in range(0,10)) and all([x > 5 for x in range(0,10)]) return the same outcome; this is because lists and tuples behave the same way when they aren't being edited. To see how the example above examines each element in a list or tuple, see the example below.
a = (x > 5 for x in range (0, 10)) # a is a generator
a.next() # repeat this command 10 times, you should see False, False, False,
False, False, False, True, True, True, True, respectively
[x > 5 for x in range (0, 10)] # this returns a list of [False, False, False,
False, False, False, True, True, True, True]
- print() - a function to print an object. Here is an example
a = 3
b = 5
print a+b
Output: 8
print "a+b=", a+b # use a comma to combine two statements
Output: a+b= 8
print "a+b="+str(a+b) # use a plus to combine two statements
Output: a+b=8
print "Hello! You are doing great!!"
Output: Hello! You are doing great!!
Note: We can use a comma or + to combine two statements. But what's the difference between these two ways? Try: print "a+b="+(a+b) and see what you get.
- sorted() - a function to sort a list of numbers. Here is an example
c = [8, 3, -7, 0, 2, -4, 1] # define a list and assign it to c
sorted(c) # sort the list in ascending order
Output: [-7, -4, 0, 1, 2, 3, 8]
sorted(c, reverse = True) # sort the list in descending order
Output: [8, 3, 2, 1, 0, -4, -7]
print c # c maintains the same
Output: [8, 3, -7, 0, 2, -4, 1]
c.sort() # methods are another way to sort a list
# just place a period after the object and follow it with the method you wish to use.
# in this example, we call the .sort method on the object c.
print c # the list is changed
Output: [-7, -4, 0, 1, 2, 3, 8]
c.sort(reverse=True)
print c # the list is changed
Output: [8, 3, 2, 1, 0, -4, -7]
Note: From the above example, you should see that using the sorted function only sorts the list of values but it doesn't change the original list. In addition, sorted() can be used to sort any object that capable of returning its members one at a time, not just a list.
- open() - a function we use to open a file. We will introduce how to use it in the section of Importing Data & Writing Files.