Python Basics
Python Basics
Python Basics
keyboard_arrow_down Contents
1. Syntax and whitespace
2. Comments
3. Numbers and operations
4. String manipulation
5. Lists, tuples, and dictionaries
6. JSON
7. Loops
8. File handling
9. Functions
10. Working with datetime
11. NumPy
12. Pandas
To run a cell, press Shift+Enter or click Run at the top of the page.
keyboard_arrow_down 2. Comments
In Python, comments start with hash '#' and extend to the end of the line. '#' can be at the begining of the line or after code.
Hello world!
# is not a comment in this case
Operation Result
x%y Remainder of x / y
abs(x) Absolute value of x
x ** y x to the power y
# Number examples
a = 5 + 8
print("Sum of int numbers: {} and number format is {}".format(a, type(a)))
b = 5 + 2.3
print ("Sum of int and {} and number format is {}".format(b, type(b)))
# Use [] to access the character of the string. The first character is indicated by '0'.
print(test_word[0])
keyboard_arrow_down Lists
A list is created by placing all the items (elements) inside square brackets [ ] separated by commas. A list can have any number of items, and
they may be of different types (integer, float, strings, etc.).
# A Python list is similar to an array. You can create an empty list too.
my_list = []
[3, 5, 7]
600
3
5
7
10
1
python
3
keyboard_arrow_down Tuples
A tuple is similar to a list, but you use them with parentheses ( ) instead of square brackets. The main difference is that a tuple is immutable,
while a list is mutable.
my_tuple = (1, 2, 3, 4, 5)
my_tuple[1:4]
(2, 3, 4)
keyboard_arrow_down Dictionaries
A dictionary is also known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to
its associated value.
123
keyboard_arrow_down 6. JSON
JSON is text writen in JavaScript Object Notation. Python has a built-in package called json that can be used to work with JSON data.
import json
keyboard_arrow_down 7. Loops
If, Else, ElIf loop: Python supports conditional statements like any other programming language. Python relies on indentation (whitespace at
the begining of the line) to define the scope of the code.
a = 22
b = 33
c = 100
if a > b:
print("a is greater than b")
elif b > c:
print("b is greater than c")
else:
print("b is greater than a and c is greater than b")
b is greater than a
b is greater than a and c is greater than b
print("="*10)
# Continue to next iteration if x is 2. Finally, print message once the condition is false.
x = 0
while x < 5:
x += 1
if x == 2:
continue
print(x)
else:
print("x is no longer less than 5")
count is 1
count is 2
count is 3
count is 4
count is 5
count is 6
count is 7
count is 8
count is 9
==========
1
3
4
5
x is no longer less than 5
For loop: A For loop is more like an iterator in Python. A For loop is used for iterating over a sequence (list, tuple, dictionay, set, string, or
range).
print("\n")
print("="*10)
print("\n")
# Iterating range
for x in range(1, 10, 2):
print(x)
else:
print("task complete")
print("\n")
print("="*10)
print("\n")
orange
banana
apple
grape
cherry
==========
1
3
5
7
9
task complete
==========
red stop
red slow down
red go
yellow stop
yellow slow down
yellow go
green stop
green slow down
green go
"r" - Read
"a" - Append
"w" - Write
"x" - Create
In addition, you can specify if the file should be handled in binary or text mode.
"t" - Text
"b" - Binary
# Read file
file = open('test.txt', 'r')
print(file.read())
file.close()
print("\n")
print("="*10)
print("\n")
print("\n")
print("="*10)
print("\n")
This is a test file with text in it. This is the first line.
This is the second line.
This is the third line.
==========
This is a
==========
This is a test file with text in it. This is the first line.
# Update file
file = open('test2.txt', 'a')
file.write("\nThis is additional content in the new file.")
file.close()
# Delete file
import os
file_names = ["test.txt", "test2.txt"]
for item in file_names:
if os.path.exists(item):
os.remove(item)
print(f"File {item} removed successfully!")
else:
print(f"{item} file does not exist.")
File test.txt removed successfully!
File test2.txt removed successfully!
keyboard_arrow_down 9. Functions
A function is a block of code that runs when it is called. You can pass data, or parameters, into the function. In Python, a function is defined by
def .
# Defining a function
def new_funct():
print("A simple function")
A simple function
def param_funct(first_name):
print(f"Employee name is {first_name}.")
param_funct("Harry")
param_funct("Larry")
param_funct("Shally")
Anonymous functions (lambda): A lambda is a small anonymous function. A lambda function can take any number of arguments but only one
expression.
print("\n")
print("="*10)
print("\n")
x = lambda a, b: a*b/100
print(x(2,4))
115
==========
0.08
import datetime
x = datetime.datetime.now()
print(x)
print(x.year)
print(x.strftime("%A"))
print(x.strftime("%B"))
print(x.strftime("%d"))
print(x.strftime("%H:%M:%S %p"))
2023-11-30 19:51:49.727931
2023
Thursday
November
30
19:51:49 PM
keyboard_arrow_down 11. NumPy
NumPy is the fundamental package for scientific computing with Python. Among other things, it contains:
(3, 5)
15
dtype('float64')
'int16'
np.add(a,b) # Addition
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.]])
np.subtract(a,b) # Substraction
np.divide(a,d) # Division
np.multiply(a,d) # Multiplication
False
105
7.0
4.320493798938574
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
a[:1] # Select all items at row 0
array([[0, 1, 2, 3, 4]])
array([0, 1])
array([[ 0, 5, 10],
[ 1, 6, 11],
[ 2, 7, 12],
[ 3, 8, 13],
[ 4, 9, 14]])
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0.])
[array([[0, 1, 2, 3, 4]]),
array([[5, 6, 7, 8, 9]]),
array([[10, 11, 12, 13, 14]])]
[array([[ 0],
[ 5],
[10]]),
array([[ 1],
[ 6],
[11]]),
array([[ 2],
[ 7],
[12]]),
array([[ 3],
[ 8],
[13]]),
array([[ 4],
[ 9],
[14]])]
keyboard_arrow_down Pandas
Pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python
programming language.
Pandas DataFrames are the most widely used in-memory representation of complex data collections within Python.
/home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages/pandas/core/computation/expressions.py:21: UserWarning
from pandas.core.computation.check import NUMEXPR_INSTALLED
# Sample dataframe df
df = pd.DataFrame({'num_legs': [2, 4, np.nan, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, np.nan, 1, 8]},
index=['falcon', 'dog', 'spider', 'fish'])
df # Display dataframe df
# Another sample dataframe df1 - using NumPy array with datetime index and labeled column
df1 = pd.date_range('20130101', periods=6)
df1 = pd.DataFrame(np.random.randn(6, 4), index=df1, columns=list('ABCD'))
df1 # Display dataframe df1
keyboard_arrow_down Viewing data
A B C D
df1 = 2013-01-01
pd.date_range('20130101',
-0.898850 -0.680102periods=6)
0.193667 1.074850
df1 = pd.DataFrame(np.random.randn(6, 4), index=df1, columns=list('ABCD'))
2013-01-02 1.431951 0.793661 0.946500 -0.507993
df1.head(2) # View
2013-01-03 top data
1.660753 1.023082 -0.578049 -1.202825
A B C D
A B C D
A float64
B float64
C float64
D float64
dtype: object
D C B A
A B C D
A B C D
A B C D
A B
A -1.732572
B 0.245337
C 0.694448
D 0.999107
Name: 2013-01-04 00:00:00, dtype: float64
df1[df1 > 0] # Select values from a DataFrame where a boolean condition is met
A B C D
A B C D E
account_circle
num_wings
falcon 2
spider
falcon 2.0 0 2 10.0
dog
fish 4.0 0 0 5.0
keyboard_arrow_down Plotting
2
3
spider
fish
NaN
0.0
0
0
1.0
8.0
2000-01-01 -0.909929
2000-01-02 -0.713175
2000-01-03 0.256578
2000-01-04 1.887163
2000-01-05 0.156225
Freq: D, dtype: float64
ts = ts.cumsum()
ts.plot() # Plot graph
plt.show()
# On a DataFrame, the plot() method is convenient to plot all of the columns with labels
df4 = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,columns=['A', 'B', 'C', 'D'])
df4 = df4.cumsum()
df4.head()
A B C D
df4.plot()
plt.show()