Unformatted text preview:

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 Explain x 3 y 3 14 name Alice is student True Functions def greet name print Hello name result add 3 4 result is now 7 def add x y return x y Control Flow Explain if statement if result 5 In Python you can define your own functions using the def keyword Here s an example greet Bob prints Hello Bob You can also define functions that return values Here s an example Python has several control flow statements including if for and while Here s an example of each 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 Python is an easy and intuitive language that is great for beginners but also powerful enough for experienced programmers With its simple syntax and vast ecosystem of libraries Python is a popular choice for web development data analysis machine learning and more 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 Explain import math 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 radius 5 area math pi radius 2 print The area of the circle is area Explain 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 Conclusion 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 Setup Python and Code Editor Environment In this chapter we will learn how to 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 head over to the Anaconda website https www anaconda com and download the latest version of Anaconda for your operating system Windows Mac or Linux After downloading run the installer and follow the prompts to complete the installation For example here s a screenshot of the Anaconda installer for Windows Step 2 Launch Jupyter Notebook Once Anaconda is installed launch Jupyter Notebook by opening Anaconda Navigator and clicking on the Jupyter Notebook icon This will open a new tab in your web browser with the Jupyter Notebook interface Here s a screenshot of the Jupyter Notebook interface Step 3 Create a New Python Notebook In Jupyter Notebook 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 Explain import math radius 5 area math pi radius 2 print The area of the circle is area Explain import numpy as np import matplotlib pyplot as plt x np linspace 0 2 np pi 100 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 y np sin x plt plot x y plt show Python Syntax for i in range 5 print i total 1 2 3 4 5 6 This is a comment print Hello world Python Data Types Explain This will generate a plot of a sine function with a range of 0 to 2 Step 5 Save and Share Notebooks Finally save your 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 Python s syntax is known for its simplicity and readability Here are some key concepts to keep in mind Indentation In Python indentation is used to define blocks of code This means that if you have a loop or a conditional statement the code that falls within that block must be indented Here s an example In this example the print i statement is indented to indicate that it belongs to the for loop Line continuation If you have a long line of code you can use the backslash character to continue it onto the next line Here s an example In this example the backslash is used to continue the addition onto the …


View Full Document

Anna GE 3151 - Introduction of Python 1

Download Introduction of Python 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction of Python 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction of Python 1 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?