Tutorial 1: A gentle Introduction to Python
A tutorial of some commonly used function in Python
Estimated time: 10 minutes
Python: the basics
Python is a general purpose programming language that supports rapid development of scripts and applications.
Why Python?
- Open Source software, supported by Python Software Foundation
- Available on all major platforms (ie. Windows, Linux and MacOS)
- It is a general-purpose programming language, designed for readability
- Supports multiple programming paradigms ('functional', 'object oriented')
- Very large community with a rich ecosystem of third-party packages
How to get started?
Using interpreter or interactive notebook, like this one. (We will not cover on how to install in your local machine)
Let's get started!
To learn a new language, is to start with printing out random string! In Python, we just call print("some strings")
. Note that in Python, we can use ""
or ''
to represent a string.
print('Hi! I am hongvin')
message = 'Hi! I am hongvin'
print(message)
a = 10
b = 5.5
c = "hi"
a,b,c
type(a),type(b),type(c)
2+2
6*7
6**7
6%3
3 > 5
True and False
myList = [1, 2, 3, 4, 5]
myList
Get the length of the list thru len()
len(myList)
Lists can contain multiple data types, including other lists
complexList = [1, 3.0, "hi", [1, 4, 5, "gg"], "MESA", myList]
complexList
But how to get the element in the list? Remember that Python is 0-indexed, therefore the first element is indexed as 0.
complexList[0]
complexList[4]
How to add to the list? You can append items to the end of the list with append()
. You can also add multiple items to the end of a list with extend()
.
myList.append(6)
myList.extend([7,8,9])
myList
for num in myList:
print(num)
Indentation is very important in Python. Note that the second line in the example above is indented, indicating the code that is the body of the loop.