search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
chevron_leftPython
Basics6 topics
Useful Libraries1 topic
Scikit-learn1 topic
Seaborn4 topics
Beautiful Soup5 topics
Functions3 topics
NumPy4 topics
Matplotlib3 topics
Environment Setup3 topics
Standard Library3 topics
Control Flow3 topics
Classes5 topics
Pandas6 topics
Built-in Functions2 topics
Operators2 topics
Operations5 topics
Getting started
check_circle
Mark as learned
thumb_up
10
thumb_down
1
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

Getting started with Python

schedule Aug 10, 2023
Last updated
local_offer
Python
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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 "
s1.strip()
'Hello World'

Splitting strings

To split a string using whitespace as the delimiter:

s ='this is a sentence'
s.split()
['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 []:

# List of fruits
fruits = ["apple", "orange","pear"]
print(fruits)
['apple', 'orange', 'pear']

Adding items

To add an item to the end of a list, use the append(~) method:

colors = ['red', 'blue']
colors.append('green')
print(colors)
['red', 'blue', 'green']

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:

x = [1, 2, 3]
y = [4, 5, 6]
print(x + y)
[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:

nums = [5, 9, 30, 22, 13]
new_nums = [num + 1 for num in nums]
print(new_nums)
[6, 10, 31, 23, 14]
fruits = ['apple', 'banana', 'pear', 'orange']

for fruit in fruits:
print(fruit)
apple
banana
pear
orange

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'] = 4
print(dict)
{'one': 1, 'two': 2, 'three': 3, 'four': 4}

Update values

To update the value of an existing key:

dict['one'] = 100
print(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)
apple
banana
pear
orange

While loop

A while loop will execute a certain piece of code as long as a certain condition remains true:

i = 0
while i < 5:
print(i, end=' ')
i += 1 # increment i by 1
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 a
multi-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

print(full_name.__doc__)
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.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...