Chapter 1: Getting Started With Python Programming
Introduction
Python is a popular programming language known for its simplicity and
versatility. In this chapter, we will go over the basics of Python programming,
including data types, variables, and functions.
Data Types and Variables
Python has several built-in data types, including:
Integers (e.g. 3, -15)
Floating point numbers (e.g. 3.14, -0.5)
Strings (e.g. "hello", 'world')
Booleans (e.g. True, False)
You can assign values to variables in Python like this:
x=3
y = 3.14
name = "Alice"
is_student = True
Functions
In Python, you can define your own functions using the def keyword. Here's an
example:
def greet(name):
print("Hello, " + name)
greet("Bob") # prints "Hello, Bob"
You can also define functions that return values. Here's an example:
,def add(x, y):
return x + y
result = add(3, 4) # result is now 7
Control Flow
Python has several control flow statements, including if, for, and while. Here's
an example of each:
# if statement
if result > 5:
print("Result is greater than 5")
# for loop
for i in range(5):
print(i)
# while loop
i=0
while i < 5:
print(i)
i += 1
we covered the basics of Python programming, including data types, variables,
and functions. With this knowledge, you can start writing your own Python
programs!
,Chapter 2: Setting Up Python and Code Editor Environment
In this chapter, we will set up our Python and code editor environment to
ensure a smooth learning journey. We will use Anaconda, a free and open-
source distribution of Python and R programming languages for scientific
computing, that aims to simplify package management and deployment.
Step 1: Download and Install Anaconda
First, let's head over to the Anaconda website (https://www.anaconda.com/)
and download the latest version of Anaconda for our operating system
(Windows, Mac, or Linux). After downloading, we can run the installer and
follow the prompts to complete the installation.
Step 2: Launch Jupyter Notebook
Once Anaconda is installed, we can launch Jupyter Notebook, a web-based
interactive computing environment, by opening Anaconda Navigator and
clicking on the Jupyter Notebook icon. This will open a new tab in our web
browser with the Jupyter Notebook interface.
Step 3: Create a New Python Notebook
In Jupyter Notebook, we can create a new Python notebook by clicking on the
"New" button in the upper right corner and selecting "Python 3". This will open
a new notebook with a blank code cell.
Step 4: Write and Execute Python Code
Now we can start writing Python code in the code cell by typing it in and
pressing "Shift + Enter" to execute the code. For example, let's say we want to
calculate the area of a circle with a radius of 5. We can write the following
code:
import math
radius = 5
area = math.pi * (radius ** 2)
print("The area of the circle is:", area)
, When we execute this code, it will print out "The area of the circle is:
78.53981633974483".
Another example is to generate a hand-drawn plot. We can use
the matplotlib library to create a simple plot of a sine function:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
This will generate a plot of a sine function with a range of 0 to 2π.
Step 5: Save and Share Notebooks
Finally, we can save our notebook by clicking on "Save and Checkpoint" in the
File menu and sharing it with others by exporting it as a Python script or a
static HTML file.
In this chapter, we learned how to set up our Python and code editor
environment using Anaconda and Jupyter Notebook. We learned how to write
and execute Python code, generate plots, and save and share our notebooks.
With this foundation, we can now move on to learning data manipulation and
analysis techniques using Python.