Getting started with Python
Start your free 7-days trial now!
Python is an open-source programming language with a wide variety of applications in data science, machine learning, and web programming. It is known for its readability and simplicity compared to other programming languages such as C++.
Essentials
Strings
Strings in Python are represented using single or double quotations:
s1 = 'Hello World's2 = "Hello World"
Concatenating strings
To concatenate strings in Python, use the +
operator:
s1 = "Sky" + "Towner"print(s1)
SkyTowner
Stripping empty spaces
To strip leading and trailing spaces, use the strip(~)
method:
s1 = " Hello World "
'Hello World'
Splitting strings
To split a string using whitespace as the delimiter:
s ='this is a sentence'
['this', 'is', 'a', 'sentence']
Lists
A list is a collection of elements in a particular order. In Python, a list is represented by square brackets []
:
['apple', 'orange', 'pear']
Adding items
To add an item to the end of a list, use the append(~)
method:
Refer here for a more comprehensive guide on ways you can add items to a list.
Finding length of a list
To find the length of a list, use the len()
method:
cars = ['mercedes', 'range rover', 'toyota', 'renault']len(cars)
4
Concatenating lists
To concatenate two lists, use the +
operator:
[1, 2, 3, 4, 5, 6]
List comprehension
List comprehension in Python allows us to create a new list based on the values of an existing list in a short, efficient manner:
[6, 10, 31, 23, 14]
fruits = ['apple', 'banana', 'pear', 'orange']
for fruit in fruits: print(fruit)
applebananapearorange
Dictionaries
A dictionary stores key-value pairs:
dict = {'one': 1, 'two': 2, 'three': 3}
Retrieve values
To retrieve values in a dictionary using their corresponding keys:
dict['two']
2
Add new key-value pair
To add a new key-value pair to the dictionary:
dict['four'] = 4print(dict)
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
Update values
To update the value of an existing key:
dict['one'] = 100print(dict)
{'one': 100, 'two': 2, 'three': 3}
For loop
You can use for loops in Python when you want to perform the same action with every item in an iterable such as a list:
fruits = ['apple', 'banana', 'pear', 'orange']
for fruit in fruits: print(fruit)
applebananapearorange
While loop
A while loop will execute a certain piece of code as long as a certain condition remains true:
0 1 2 3 4
Comments
Single-line
To write a single-line comment in Python:
# I am a comment.
Multi-line
Python does not have formal syntax for multi-line comments. The recommendation by PEP8 is to use consecutive single line comments:
# I am# a multi-line comment
You can achieve something of similar effect using a multi-line string wrapped in """
:
"""I am amulti-line string."""
However, do note that when the triple quote syntax is used as part of a function / class / module it serves as a docstring, providing documentation on that particular function / class / module:
def full_name(fname, lname): """Returns the full name based on provided first and last names""" return fname + lname
Returns the full name based on provided first and last names
External Libraries
Python has many open-source libraries that can be imported with ease to equip you with powerful tools for data analysis and web programming. Below we introduce a few of the most frequently used libraries:
Pandas
Pandas is the most popular open-source library for data analysis in Python. The library provides you with simple but powerful data structures that easily allows you to manipulate data at ease. Check out Getting Started with Pandas to learn more about the library.
NumPy
NumPy, which stands for Numerical Python, is a popular library for Python that is used for numerical computations. It is extremely performant in terms of computational speed and memory consumption compared to the standard Python. This is because NumPy densely packs data of the same type as an array - this is in contrast to standard Python lists that hold different data types at different memory locations. Check out Getting Started with NumPy to learn more about the library.
Matplotlib
Matplotlib is a data visualization library in Python. Within the library, the pyplot
module is commonly used for easily creating highly customizable graphs and figures to visualize your data. Check out Getting Started with Matplotlib to learn more about the library.
Beautiful Soup
Beautiful Soup is a Python library that allows you to retrieve desired data from HTML and XML. It is often used for web scraping processes that retrieve data from websites. Checkout out Getting Started with Beautiful Soup to learn more about the library.