This document is one of More SageMath Tutorials. You may edit it on github. \(\def\NN{\mathbb{N}}\) \(\def\ZZ{\mathbb{Z}}\) \(\def\QQ{\mathbb{Q}}\) \(\def\RR{\mathbb{R}}\) \(\def\CC{\mathbb{C}}\)
First steps towards programming¶
In this worksheet, you will learn to define functions, write for loops, conditional statements and and continue to learn about lists.
Functions¶
To define a function in Sage, use the def
command and a colon after the
variable names. For example:
sage: def is_even(n):
....: return n % 2 == 0
Notice that body of the function (the line: return n % 2 == 0
) is
indented. The indentation defines the body of the function.
Exercises:
Define a function called
square
that returns the square of a number:sage: # edit here
If statement¶
Below is an example of an if
statement:
sage: if n % 2 == 0:
....: return True
....: else:
....: return False
Notice again how the commands in the first block and the second block of the statement are indented.
The following example defines the factorial_function
, which takes a
number \(n\) and returns the product \(n(n-1)(n-2)\cdots1\):
sage: def factorial_function(n):
....: if n == 0:
....: return 1
....: elif n == 1:
....: return 1
....: else:
....: return n*factorial_function(n-1)
Exercises:
Define a function
sign
that returns the sign of a number:sage: # edit here
For loops¶
The following example uses a for loop to create a list of all the numbers between 1 and 1000 that are multiples of 3 and 5:
sage: nums = []
sage: for i in range(1,1001):
....: if i % 3 == 0 and i % 5 == 0:
....: nums.append(i)
sage: print nums
[15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 495, 510, 525, 540, 555, 570, 585, 600, 615, 630, 645, 660, 675, 690, 705, 720, 735, 750, 765, 780, 795, 810, 825, 840, 855, 870, 885, 900, 915, 930, 945, 960, 975, 990]
Exercises:
Recall that the Fibonacci sequence is the sequence of numbers that begins with \(F_0 = 0\), \(F_1=1\), and that satisfies the equation \(F_n = F_{n-1} + F_{n-2}\) for all \(n\geq2\). Define a function that returns a list of the first
m
terms in the Fibonacci sequence:sage: # edit here
Project Euler Problem 2¶
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
sage: # edit here
Slicing lists¶
You can slice a list to obtain only part of it. The syntax is
L[start:stop:step]
.
Let
L = range(100)
, and try the followingL[0:3], L[:3], L[1:], L[1:-1], L[::2]
.sage: # edit here
Create a list
L
.sage: # edit here
Use a slice to obtain the reversal of
L
.sage: # edit here
Revese the list
L
usingL.reverse()
.sage: # edit here
What is the difference between these methods of reversing a list?
sage: # edit here
Project Euler Problem 4¶
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is \(9009 = 91 \times 99\).
Find the largest palindrome made from the product of two 3-digit numbers.
Hints:
7%3
returns the remainder of 7 divided by 3.7//3
returns the integer quotient of 7 by 3.
sage: # edit here