Title | CS101 Notes |
---|---|
Author | Corey Wong |
Course | Intro Computing: Engrg & Sci |
Institution | University of Illinois at Urbana-Champaign |
Pages | 6 |
File Size | 217.4 KB |
File Type | |
Total Downloads | 32 |
Total Views | 127 |
Notes on how to code in python. It has a wide variety of different things such as while and for loops. It also has notes on how to plot using numpy and sympy in python....
CS 101 - Notes
Python: Several elements of Python syntax can be identified in this program:
● ● ● ● ●
literals are the numbers and the text strings (blue and red) names are variables or functions, like print and C operators like + and = calculate values comments are here italicized and are not interpreted by the machine keywords are in bold (not visible in this particular program)
During this lesson, we will introduce several examples of each of these elements. Depending on the editor you are using, you may not have the visual clues of typeface weight or color to help you identify elements. In order to identify a component of a program correctly, you need to think about the rôle the component plays:
● ● ● ● ●
literals represent exact information (we call this hard-coded ) keywords represent commands or structural indicators names represent values or functions that we want to use operators combine values, whether from literals, names, or compound expressions comments represent explanations we use to explain our code
Math Symbols ● ● ● ● ●
+, -, *, / arithmetic ** exponentiation % modulus (remainder) // floor division = assignment
Python similarly distinguishes three basic numeric types:
● ● ●
int, the integers, numbers without any decimal part (even zero) float, floating-point numbers, numbers with a decimal part complex, complex numbers, any number with an imaginary part represented (including 0 j).
(Python uses j to represent sqrt(-1) instead of i.
In [1]: import math In [2]: math.
math.acos math.acosh math.asin math.asinh
math.atan math.atan2 math.atanh math.ceil
math.copysign math.cos math.cosh math.degrees
math.e math.erf math.erfc math.exp
math.expm1 math.fabs math.factorial > math.floor
Math Symbols:
<
less than
>
greater than
=
greater than or equal to
==
equal to
!=
not equal to
and
(both sides are True)
or
(either side is True)
not
(flip the truth-value)
in
(for containers like strings, check membership)
is
(check identity)
Arrow. Indicates control flow, or the way that the program can in principle execute.
Process Block. Shows a set of operations, typically statements or functions.
Decision Diamond. Shows a choice to be made with at least two possibilities, typically an if block.
Terminal. Indicates the end of a program. May be shown once or many times by convenience.
Input/Output. Indicates a need for user input (often the input command) or output.
The while Loop Body
A while loop has two main pieces:
1. A header line with a condition (True/False) 2. A loop body A while loop needs something to change for iteration to cease. This means that variables set up before the loop begins may (and should) change while inside of the loop body. Each time through, something happens inside the loop body which may make it possible for the while loop to exit the next time it checks the condition.
The for Loop Body A for loop has three main pieces: 1. A loop variable, sometimes called an index variable (if a number) or a dummy variable. 2. A set of things to iterate over (a string, a rang, a container) 3. A loop body The loop variable assumes in turn each value in the set of things to iterate over. This means that each time through the loop body, the loop variable changes its value.
# 1. Open the file to read. myfile = open( 'words.txt','r' ) # 2. Read all of the data into memory. mydata = myfile.read() # 3. Close the file. myfile.close() # 4. Use the data which were loaded. print( mydata )
Exception
Interpretation
SyntaxError
Invalid program.
NameError
Unknown variable name.
TypeError
Invalid operation on a value.
ValueError
Invalid value for a function.
ZeroDivisionError
Division by zero.
FileNotFoundError
File is missing or inaccessible.
IndexError
Attempting to access index outside of list bounds.
KeyError
Attempting to access key that doesn't exist in dict.
IndentationError
Not correctly using four spaces for indentation.
Exception
Generic category of errors.
The common plot modifiers you'll use include:
● ● ● ● ● ●
title legend (together with the label keyword argument) xlim ylim xlabel ylabel
We can display information in several formats using short format strings, passed in to the plot command as a color–marker pair.
plt.plot( x,y,'ro' ) plt.xlim( 0,2*np.pi ) plt.ylim( -1,1 ) plt.show() plt.plot( x,y,'c--',linewidth=2 ) plt.xlim( 0,2*np.pi ) plt.ylim( -1,1 ) plt.show()
Color
String
Marker Type
String
Red
'r'
Solid line
'-'
Green
'g'
Dashed line
'--'
Blue
'b'
Circles
'o'
Black
'k'
Dots
'.'
Magenta
'm'
Crosshatches
'x'
Yellow
'y'
Crosses
'+'
Cyan
'c'...