Introduction
|
|
Starting Python in the DIVAS VM
|
Spyder is an Integrated Development Environment (IDE) that makes it easier to write and debug code
You can write code one line at a time in the console or write longer scripts in the Editor window
Code should be written in an IDE like Spyder or by using a plain text editor
|
Variables
|
Creating variables allows you to “reuse” a number or value in multiple locations in your code
Python stores different kinds of information (text, numbers, decimal numbers, and others) as different “types” of variables
You should give your variables descriptive names! This will make your code easier to read!
|
Running code in the editor
|
A script is a file that contains multiple lines of code
A script can be run all at once rather than writing and running one line at a time
A script can be reused over and over again
|
Lists and Indexing
|
Lists can be used to group numbers (ints or floats), strings, lists, or other kinds of values together in one variable
Lists are ordered- objects stay in the order you add them
Lists are indexed using 0-based indexing
You can use square bracket notation to return a single item in a list or subset of items
|
For Loops
|
Loops require careful attention to spacing and indentation
The loop variable takes on the value of each item in the object we are looping over, one at a time
You must refer to the loop variable in the code written under the for loop statement
|
Conditionals
|
If/elif/else statements allow your program to do different things if different conditions are met
If/elif/else blocks require careful attention to indentation
In an if/elif/else block, the code underneath the first condition that is true will be executed
Code under other conditions will not be executed, even if they are also true
|
Reading and Writing Text Files
|
Opening and reading a file is a multistep process: Defining the filename, opening the file, and reading the data
Data stored in files can be read in using a variety of commands
Writing data to a file requires attention to data types and formatting that isn’t necessary with a print() statement
|
Analyzing Data with Numpy
|
Libraries contain specialized functions that can be imported and used in your code
The numpy library contains functions that allow you to easily read in a .csv-formatted data file and perform simple operations on rows and columns of the data
|
Defining Functions
|
Function statements start with the keyword def, and must include a name, parameters, and a return statement
Functions must be called in order to use them
Functions can be imported to be used by other scripts using the import command
|