Attachment 'chap3-if.rst'

Download

Chapter 3: if statements

Authors: Vincent Delecroix
License:CC BY-SA 3.0

Comparisons and if

The comparison signs in Python and many other programming languages are as follows

== equality
!= difference
< less than
> greater than
<= lesser than or equal to
>= greater than or equal to

Exercise 3.1

Which number is the largest 10001001 or 10011000?

Exercise 3.2

Let us consider the following code:

sage: a = # enter a value for a
....: if a != 2:
....:     print('lost')
....: elif a == 3:
....:     print('an instant, please')
....: else:
....:     print('you win')

What is the above program doing

  • when the variable a is 1?
  • when the variable a is 2?
  • when the variable a is 3?
  • when the variable a is 15?

Exercise 3.3

Two prime numbers p and q are said twin if q = p + 2. Find all twin prime numbers below 10000.

Exercise 3.4

Find the smallest and largest integers in the set

{ab − ba : a ∈ {1, 2, …, 5}, b ∈ {1, 2, …, 5}}

Exercise 3.5

Recall that the method digits of an integer returns the list of its digits:

sage: 1527.digits()

Solve Euler problem 56 by finding the maximal sum of digits of numbers of the form ab with both a and b lesser than 100

Exercise 3.6

Solve Euler problem 4 about palindromes.

Exercise 3.7

Let us consider the following list of integers:

sage: l = [123, 414, 264, 18, 689, 21, 5571, 28, 589, 12, 111, 231,
....: 158, 551, 250, 68, 5728, 2222, 4198, 571, 28, 518, 999, 444,
....: 112, 689, 672, 334, 680, 273]

Construct two lists leven and lodd that contain respectively the even and odd elements of l.

Using in and not in

The condition of an if or elif statement is not necessarily a comparison. Basically, any Python object would fit!

sage: a = 5
sage: if a:
....:     print('I am not zero')

What happens under the hood is that the object a (here an integer) is converted to a boolean value (True or False). You can see the boolean value of an object by using bool

sage: bool(5)
sage: bool(0)
sage: bool([])
sage: bool([0])

A useful construction is obtained with the keyword in: the result of a in b is whether a belongs to the object b. For example:

sage: 2 in ZZ
sage: 2/3 in ZZ
sage: 2/3 in QQ
sage: 1 in [3, 5, 2, 1, 2, 8]
sage: 'a' in 'Saint-Flour'
sage: 'z' in 'Saint-Flour'

To check that an element is not in a given object use a not in b:

sage: 10 not in Primes()
sage: 5/2 not in ZZ

Exercise 3.8

Using an if statement involving in inside a for loop, count the number of vowels in the string:

sage: s = 'How many vowels are present in this sentence?'

Count the number of consonant in the string:

sage: s = 'How many consonants are present in this sentence?'

Exercise 3.9 (Pythagorean triples)

A Pythagorean triple is a triple (a, b, c) of positive integers so that a2 + b2 = c2. An example is 32 + 42 = 52. How many Pythagorean triples are there with a, b and c smaller than 100?

Solve Euler problem by finding the unique Pythagorean triple so that a + b + c = 1000

Combining conditions or, and and not

To make even more complicated tests you can combine them. The main operators for this are or, and.

sage: n = 17
sage: if n.is_prime() and (n+2).is_prime():
....:     print('a twin number!')

Exercise 3.10

Let us call a positive integer n a triple twin if all of n, n+2 and n+6 are primes. How many triple twins are there smaller than 10000?

The operator not is used for negation of a condition.

sage: not True
sage: not False

More exercises

For more exercises in the same veine you can challenge yourself with

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-08-12 22:01:42, 8.3 KB) [[attachment:CollatzConjecture.ipynb]]
  • [get | view] (2018-08-12 22:01:45, 100.9 KB) [[attachment:CollatzConjecture.pdf]]
  • [get | view] (2018-08-12 22:01:49, 4.8 KB) [[attachment:CollatzConjecture.rst]]
  • [get | view] (2018-08-12 22:01:53, 12.3 KB) [[attachment:Dictionaries-GraphTheory.ipynb]]
  • [get | view] (2018-08-12 22:01:56, 202.6 KB) [[attachment:Dictionaries-GraphTheory_Solutions.pdf]]
  • [get | view] (2018-08-12 22:02:00, 9.1 KB) [[attachment:Dictionaries-GraphTheory_Solutions.rst]]
  • [get | view] (2018-08-15 12:32:58, 16.0 KB) [[attachment:Fields-2018-flatsurf_and_surface_dynamics_demo.ipynb]]
  • [get | view] (2018-08-11 19:25:37, 15.0 KB) [[attachment:S_2_1.svg]]
  • [get | view] (2018-08-12 22:02:10, 13.4 KB) [[attachment:Strings-BWT.ipynb]]
  • [get | view] (2018-08-12 22:02:14, 72.4 KB) [[attachment:Strings-BWT.pdf]]
  • [get | view] (2018-08-12 22:02:17, 7.0 KB) [[attachment:Strings-BWT.rst]]
  • [get | view] (2018-08-11 19:28:42, 16.7 KB) [[attachment:chap1-first_steps.ipynb]]
  • [get | view] (2018-08-11 19:28:46, 103.1 KB) [[attachment:chap1-first_steps.pdf]]
  • [get | view] (2018-08-11 19:28:53, 7.8 KB) [[attachment:chap1-first_steps.rst]]
  • [get | view] (2018-08-11 19:29:05, 24.6 KB) [[attachment:chap1-first_steps_solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:09, 108.7 KB) [[attachment:chap1-first_steps_solutions.pdf]]
  • [get | view] (2018-08-11 19:29:12, 12.0 KB) [[attachment:chap1-first_steps_solutions.rst]]
  • [get | view] (2018-08-11 19:29:17, 30.8 KB) [[attachment:chap2-list_and_for.ipynb]]
  • [get | view] (2018-08-11 19:29:23, 110.0 KB) [[attachment:chap2-list_and_for.pdf]]
  • [get | view] (2018-08-11 19:29:26, 17.6 KB) [[attachment:chap2-list_and_for.rst]]
  • [get | view] (2018-08-11 19:29:32, 42.0 KB) [[attachment:chap2-list_and_for_Solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:35, 116.1 KB) [[attachment:chap2-list_and_for_Solutions.pdf]]
  • [get | view] (2018-08-11 19:29:38, 25.6 KB) [[attachment:chap2-list_and_for_Solutions.rst]]
  • [get | view] (2018-08-11 19:29:51, 21.1 KB) [[attachment:chap3-if-solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:58, 95.4 KB) [[attachment:chap3-if-solutions.pdf]]
  • [get | view] (2018-08-11 19:29:55, 9.0 KB) [[attachment:chap3-if-solutions.rst]]
  • [get | view] (2018-08-11 19:29:41, 11.8 KB) [[attachment:chap3-if.ipynb]]
  • [get | view] (2018-08-11 19:29:45, 91.6 KB) [[attachment:chap3-if.pdf]]
  • [get | view] (2018-08-11 19:29:48, 5.7 KB) [[attachment:chap3-if.rst]]
  • [get | view] (2018-08-11 19:30:02, 4.4 KB) [[attachment:chap4-functions.ipynb]]
  • [get | view] (2018-08-11 19:30:06, 83.1 KB) [[attachment:chap4-functions.pdf]]
  • [get | view] (2018-08-11 19:30:09, 2.3 KB) [[attachment:chap4-functions.rst]]
  • [get | view] (2018-08-11 19:30:20, 3.2 KB) [[attachment:chap5-while.ipynb]]
  • [get | view] (2018-08-11 19:30:14, 62.1 KB) [[attachment:chap5-while.pdf]]
  • [get | view] (2018-08-11 19:30:39, 1.5 KB) [[attachment:chap5-while.rst]]
  • [get | view] (2018-08-11 19:30:47, 4.0 KB) [[attachment:chap6-advanced_exercises.ipynb]]
  • [get | view] (2018-08-11 19:30:53, 69.7 KB) [[attachment:chap6-advanced_exercises.pdf]]
  • [get | view] (2018-08-11 19:31:00, 2.0 KB) [[attachment:chap6-advanced_exercises.rst]]
  • [get | view] (2018-08-12 22:02:04, 90.9 KB) [[attachment:euler.png]]
  • [get | view] (2018-08-13 21:52:57, 1.1 KB) [[attachment:flipper_nf_conversion.py]]
  • [get | view] (2018-08-15 12:18:55, 55.4 KB) [[attachment:flipper_tutorial.pdf]]
  • [get | view] (2018-08-12 21:56:57, 16.1 KB) [[attachment:floating_point_and_stability.ipynb]]
  • [get | view] (2018-08-12 21:57:01, 78.0 KB) [[attachment:floating_point_and_stability.pdf]]
  • [get | view] (2018-08-12 21:57:04, 6.6 KB) [[attachment:floating_point_and_stability.rst]]
  • [get | view] (2018-08-12 22:02:07, 17.6 KB) [[attachment:graph0.png]]
  • [get | view] (2018-08-11 23:44:38, 23.2 KB) [[attachment:intro.en.ipynb]]
  • [get | view] (2018-08-11 23:44:45, 117.5 KB) [[attachment:intro.en.pdf]]
  • [get | view] (2018-08-11 23:44:53, 12.5 KB) [[attachment:intro.en.rst]]
  • [get | view] (2018-08-12 23:25:35, 62.3 KB) [[attachment:logistic_orbit_interact.png]]
  • [get | view] (2018-08-11 19:48:34, 9.6 KB) [[attachment:random_walk.ipynb]]
  • [get | view] (2018-08-11 19:48:38, 87.9 KB) [[attachment:random_walk.pdf]]
  • [get | view] (2018-08-11 19:48:44, 5.7 KB) [[attachment:random_walk.rst]]
  • [get | view] (2018-08-15 19:25:46, 67.3 KB) [[attachment:real_and_complex_numbers.ipynb]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.