Title | 1 - Python Essentials - Taught by Dr Marcel Scharth |
---|---|
Author | Vincent Nguyen |
Course | Statistical Learning and Data Mining |
Institution | University of Sydney |
Pages | 10 |
File Size | 253.9 KB |
File Type | |
Total Downloads | 112 |
Total Views | 149 |
Taught by Dr Marcel Scharth...
Python for Business Analytics Python Essentials This notebook is a concise introduction to the very basics of the Python language that we wil Content: Getting started Modules Boolean and numeric variables Strings Data structures Iteration Defining functions
Getting started To get started, you can use your Notebook as a calculator. For example: In [1]: 2 + 3 Out[1]: 5 In [2]: 10/4 Out[2]:
In [3]: x = 5 The print function allows us to display information. In [4]: print('For truth is always strange; stranger than fiction.') # L o r d B y n ( t h e # s t a r c o m e n ) For truth is always strange; stranger than fiction. We can also use the print function to output values. In [5]: x = 10 print(x) 10
Modules The Python language by design has a small core. Most of the fuctionality that we need is in m packages that we need to explicity load into our session. There are two ways to do this: eithe the entire package (or a subset of it) or a specific function. In [6]: import numpy as np # t h e n p i s a l np.sqrt(4) Out[6]: 2.0 In [7]: from numpy import sqrt sqrt(4) Out[7]: 2.0
In [8]: x = False print(x) False In [9]: x = 2 > 0 print(x) True In [10]: type(x) Out[10]: bool In expressions involving numbers, a False is automatically converted to zero and a True is one. In [11]: y = 2*x print(y) 2 The basic numerical data types are integers and floats. In [12]: x = 2 type(x) Out[12]: int In [13]: x = 2.0 type(x) Out[13]:
In [14]: sentence = 'For truth is always strange; stranger than fiction.' type(sentence) Out[14]: str We now take strings as an example to explore some of the basic functionality available in Pyt In [15]: sentence.lower() Out[15]: 'for truth is always strange; stranger than fiction.' Above, lower is a method available for the string object. A method is a function that operate associated object. In [16]: 'truth' in sentence # c h e k s w t r u b i n g ' a p e n c Out[16]: True In [17]: sentence = 'It is sunny today' print(sentence) new_sentence = sentence.replace('sunny', 'raining') print(new_sentence) It is sunny today It is raining today In [18]: len(sentence) # h o w m a n y c r t e s i g v b l ? Out[18]: 17
Data structures Lists A list is a sequence of values. The individual elements or items of a list can be of any type (inc list!). In [20]: x =[1, 'Science', True] type(x) Out[20]: list In [21]: print(x) [1, 'Science', True] In [22]: felines = [] print(felines) [] In [23]: felines.append('lion') felines.append('leopard') print(felines) ['lion', 'leopard'] In [24]: big_five = felines + ['elephant','rhino', 'buffalo'] print(big_five) ['lion', 'leopard', 'elephant', 'rhino', 'buffalo'] In [25]: marine = ['shark', 'salmon', 'dolphin']
In [26]: animals = [big_five, marine] print(animals) [['lion', 'leopard', 'elephant', 'rhino', 'buffalo'], ['shark', 's almon', 'dolphin']] We can access the elements of the list by indexing and slicing. Python uses zero-based index that the first element is at index zero. In [27]: big_five[0] # f i r s t e l m n Out[27]: 'lion' In [28]: big_five[3] # f o u r t h e l m n Out[28]: 'rhino' In [29]: big_five[-1] # l a s t e m n Out[29]: 'buffalo' In [30]: animals[1] # t h e s c o n d l m f a i Out[30]: ['shark', 'salmon', 'dolphin'] In [31]: animals[1][1] # g e t h s c o n d f l m a i , o f t h e r s u l Out[31]: 'salmon'
In [32]: big_five[:2] # f i r s t w o e l m n Out[32]: ['lion', 'leopard'] Note that when slicing, the element at the endpoint is not included, which may initially feel counterintuitive if you are not used to zero based indexing. In [33]: big_five[1:2] Out[33]: ['leopard'] In [34]: big_five[-2:] # l a s t w o e m n Out[34]: ['rhino', 'buffalo'] We can delete elements from a list in different ways. In [35]: big_five.remove('buffalo') big_five Out[35]: ['lion', 'leopard', 'elephant', 'rhino'] In [36]: del(big_five[-1]) big_five Out[36]: ['lion', 'leopard', 'elephant']
Dictionaries
In [37]: # 2 0 1 6 p o u l a t i n s population = {'Sydney' : 5005358, 'Melbourne' : 4641636} # H e r , S y d n a M l b o u r n e a t h k y s type(population) Out[37]: dict In [38]: population['Sydney'] Out[38]: 5005358 In [39]: population['Melbourne'] Out[39]: 4641636 In [40]: population['Brisbane'] = 2349699 print(population) {'Sydney': 5005358, 'Melbourne': 4641636, 'Brisbane': 2349699}
Iteration Iteration allows us to run a block of code repeatedly. In [41]: for animal in big_five: print(animal) lion leopard elephant In [42]:
In [43]: for city in population: print(population[city]) 5005358 4641636 2349699 In [44]: for i in range(5): print(i) 0 1 2 3 4 Here is a more complex example. In [46]: numbers = np.arange(0,12,2) print(numbers) [ 0
2
4
6
8 10]
In [47]: total = 0 for number in numbers: print(number) total += number # s u m t h e n b r o a l , q i v = a l + n u m b e r print('Total = {0}'.format(total)) 0 2 4 6 8 10 Total = 30
Defining functions
In [49]: def area(radius): return math.pi*radius**2 # t h e d o u b l a s r i k p w area(1) Out[49]: 3.141592653589793 In [50]: area(2) Out[50]: 12.566370614359172...