

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Python quick learning this type file also a student student very well and learn some python language
Typology: Lecture notes
1 / 89
This page cannot be seen from the preview
Don't miss anything!
Python Review. Modified slides from Marty Stepp and Moshe Goldstein
Some source code editors pop up the console as an external window, and others contain their own console window.
Programming basics
The Python Interpreter
Expressions
Examples: 1 + 4 * 3 42
+ - * / addition, subtraction/negation, multiplication, division % modulus, a.k.a. remainder ** exponentiation
* / % ** have a higher precedence than + -
1 + 3 * 4 is 13
Parentheses can be used to force a certain order of evaluation. (1 + 3) * 4 is 16
Real numbers
Examples: 6.022 -15.9997 42.0 2.143e
The / produces an exact answer: 15.0 / 2.0 is 7. The same rules of precedence also apply to real numbers: Evaluate ( ) before * / % before + -
Example: 1 / 2.0 is 0.
The conversion occurs on a per-operator basis. 7 / 3 * 1.2 + 3 / 2 2 * 1.2 + 3 / 2 2.4 + 3 / 2 2.4 + 1 3.
Math commands
from math import *
Command name Description abs( value ) (^) absolute value ceil( value ) (^) rounds up cos( value ) (^) cosine, in radians floor( value ) (^) rounds down log( value ) (^) logarithm, base e log10( value ) (^) logarithm, base 10 max( value1 , value2 ) (^) larger of two values min( value1 , value2 ) (^) smaller of two values round( value ) (^) nearest whole number sin( value ) (^) sine, in radians sqrt( value ) (^) square root
Constant Description e (^) 2.7182818... pi (^) 3.1415926...
Variables
Usage: Compute an expression's result, store that result into a variable, and use that variable later in the program.
Syntax:
name = value
Examples: x = 5 gpa = 3.
A variable that has been given a value can be used in expressions. x + 4 is 9
Example
Example: print Statement
You can assign (store) the result of input into a variable. Example: age = input("How old are you? ") print "Your age is", age print "You have", 65 - age, "years until retirement" Output: How old are you? 53 Your age is 53 You have 12 years until retirement
input
The for loop
for loop : Repeats a set of statements over a group of values.
Syntax: for variableName in groupOfValues : statements We indent the statements to be repeated with tabs or spaces. variableName gives a name to each value, so you can refer to it in the statements. groupOfValues can be a range of integers, specified with the range function.
Example: for x in range(1, 6): print x, "squared is", x * x
Output: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25
Cumulative loops
sum = 0 for i in range(1, 11): sum = sum + (i * i) print "sum of first 10 squares is", sum
Output: sum of first 10 squares is 385
if
Syntax: if condition : statements
gpa = 3. if gpa > 2.0: print "Your application is accepted."