To edit code, it is easiest to open two windows. Edit the code in one window and execute it in another window. On Socweb: after mapping your I-drive, you can open/edit/save your files on the I-drive. Use Notepad++ or UltraEdit for editing. (Don't use Notepad because copy and paste might create invisible control characters.) Then open the ssh window and use it for executing your code. In the ssh window, use uparrow to repeat the previous command.
# This program adds and multiplies two numbers
number1 = input("Input the first number ")
number2 = input("Input the second number ")
result = number1 + number2
print "The sum is:", result
result = number1 * number2
print "The product of", number1, "and", number2, "is:", result
To execute the python script copy the blue text from the screen
and paste it into a file. Save the file, for example, under the
name "add.py". Then type at the Unix prompt:
python add.py
# | indicates a comment; the line is ignored |
input | reads from the keyboard |
number1, number2, result | variables |
+ - * / | numeric operators |
= | assignment operator |
command | |
, | separates elements in a print statement |
print ... , | A comma at the end of a print statement suppresses newline |
from sets import Set students = Set(['Joe', 'Jane', 'Mary', 'Pete']) programmers = Set(['Caroline', 'Tom', 'John', 'Pete']) designers = Set(['Paul', 'Mary', 'Susan', 'Pete']) print "These are the students:" for student in students: print student, print "!" print "There are", len(students), "students." print people = students | programmers | designers # union print "All people:", people print student_designers = students & designers # intersection print "Students who also design:", student_designers print non_design_students = students - designers # difference print "Students who do not design:", non_design_studentsNote: do not name this file "sets.py". The reason is because "sets.py" is the name of a system file that you are importing from. Using a predefined name, will not work. Furthermore, Python may create a file "sets.pyc" in your directory, which will also interfere with the predefined name and needs to be deleted.
len(s) | cardinality of set s |
x in s | test x for membership in s |
x not in s | test x for non-membership ins |
s.issubset(t) | test whether every element in s is in t |
s.issuperset(t) | test whether every element in t is in s |
s | t | Union: new set with elements from both s and t |
s & t | Intersection: new set with elements common to s and t |
s - t | Difference: new set with elements in s but not in t |
s ^ t | Symmetric difference: new set with elements in either s or t but not both |
s.copy() | new set with a shallow copy of s |
s.add(x) | add element x to set s |
s.discard(x) | remove element x from set s |
s.clear() | remove all elements from set s |
s == t | test whether sets s and t are equal |
s != t | test whether sets s and t are not equal |
from sets import Set odd = Set([1,3,5]) even = Set([2,4,6]) copy1 = odd copy2 = odd.copy() print "Are copy1 and odd the same?", copy1 is odd print "Are copy1 and odd equal?", copy1 == odd print "Are copy2 and odd the same?", copy2 is odd print "Are copy2 and odd equal?",copy2 == odd odd.add(7) print "odd:", odd print "copy1:", copy1 print "copy2:", copy2
from sympy import isprime, SymbolFor efficiency it is best to only import what you really need.
3) This code prints the factors of numbers smaller than 10:
has_factor = False
for number in range(1,11):
for factor in range(2,number):
if number % factor == 0: ### the remainder is 0
print factor,
has_factor = True
if has_factor == True:
print "factor of", number
has_factor = False
Change the code so that it prints prime numbers.
4) Find a number n so that n**2 + n + 41 is not a prime number. Hint: use the subs() function. Use for x in range(35,45) and the sympy function isprime().
5) Write a script that calculates the Fibonacci numbers below 100. Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... The next Fibonacci number is calculated by adding the previous two numbers: 0 + 1 = 1; 1 + 1 = 2; 1 + 2 = 3; 2 + 3 = 5; ...
To terminate the calculation, you can use: while number < 100:.
6) Write a script that computes all two element subsets of a set. (Optional: think about how to write an algorithm that computes all subsets of a set. This is actually quite difficult! A set with n elements has 2n subsets. Thus the number of subsets grows quickly. You have probably not yet learned enough Python to implement this. Thus, for now just think about it. What algorithm do you use when you write down all subsets of a set with pen and paper?)