1. Bibliography

1.1. In English

1.2. În Romanian


2. Introduction

What is python? ::

What does the term language mean in the context of computers? ::

What does programming imply?

Programming language?

Program execution?

What is an shell?


3. Dialoguing with Python

3.1. Starting the shell

Both under Windows and under Linux we could use IDLE (An Integrated DeveLopment Environment for Python). This is accessible from the menus of both operating systems.

(Furthermore under Linux we could use also the built-in Python shell python, or ipython.)

Once started, Python informs us about the current version, and displays a prompt (a sequence of characters that indicates to the user that the application waits for commands):

   1 #Python 2.6 ...
   2 >>>

3.2. "Pocket calculator"

Although we have in our hands a very complex and powerful application, we can "downgrade" it and use it as a simple pocket calculator.

But before continuing we must be aware of a few issues:

Simple operations:

Prioritized operations:

Operations with complex numbers:

3.3. Numeric data types

3.4. Arithmetic operators

3.5. Arithmetic functions

3.6. Variables

/!\ In contrast to other programming languages, in Python there is no need of declaring variables, instead these are automatically defined in the moment of assignment.

   1 >>> pi = 3.14
   2 >>> radius = 7
   3 >>> perimeter = 2 * pi * radius
   4 >>> perimeter
   5 43.96
   6 >>> surface = pi * radius ** 2
   7 >>> surface
   8 153.86

3.7. Other mathematical functions

There are some useful functions that are held in the mathematical module (see the documentation for a complete list).

Importing the functions:

Using the functions:


4. Programming in Python

4.1. Source code editing

For IDLE: File -> New Window (Control-N) or File -> Open... (Control-O).

For Linux in the command line: nano <path-of-file>.

(The extension for Python files is .py.)

4.2. Source code execution

For IDLE: Run -> Run Module (F5.

For Linux in the command line: python <path-of-file>.

4.3. Comments

Generic syntax:

5. Outputting to the console

Generic syntax:

Examples:

6. Control structures

6.1. Conditional structure

/!\ Also in contrast to other programming languages, in Python correct (consistent) indentation (the spaces at the beginning of lines) is obligatory. It is recommended to use 4 spaces for each indentation level, or a single tab character. However it is not recommended to mix the two styles.

Generic syntax:

The syntax without the else clause:

The syntax for multiple conditions:

6.1.1. Examples

Testing if a number is even:

Simulating the absolute function abs:

6.2. Iterative structure

Iteration over an interval:

Iteration over an interval with a step:

Iteration over a list:

6.2.1. Examples

The sum of numbers from 1 to 100 :

Displaying the members of a list:

6.3. Repetitive structure

Generic syntax:

6.3.1. Examples

The sum of the numbers from 1 to 100 :


7. Lists

Creating lists:

Inputing lists:

Accessing and updating elements:

Adding an element at the end of a list:

Reversing the content of a list:


8. Functions

Generic syntax:

Example (returning a list of digits of a number):


9. Problems

9.1. Coversions

  1. Write a program that converts an angle from radians to degrees.
  2. Write a program that converts an angle expressed in degrees in decimal form, into the following components: degrees (integer value), minutes, seconds, tens and hundreds of seconds.

9.2. Conditional structures

  1. A point in 2D space is given by its coordinate x and y. Write a program that determines if the point is in the axis origin, or in the quadrants numbered from 1 to 4 (in reverse clock order), or if the point is on the border of the quadrants.
  2. Considering a sphere expressed as its center coordinate and its radius, and given an arbitrary point in space, it should be determined its relative position to the sphere (i.e. inside, outside or on the surface).
  3. Write a program that determines the ideal body weight of a person, given the waist, age an gender. The formula are:
    • $ G_m = 50 + 0.75 * (waist - 150) + 0.25 * (age - 20) $

    • $ G_f = G_m - 10 $

  4. Given the current date expressed as three integer numbers (year, month, and day), compute how many hours have elapsed since the beginning of the year, and how many hours are left until the end of the year. (We can consider that February has only 28 days.)
  5. Given the current date expressed as three integer numbers (year, month, and day), and a persons birth-date expressed the same way, write a program that computes the age of the person, expressed as a number real number. (We consider that February has 28.25 days.)

9.3. Iterative structures

  1. Write a program that determines the greatest common divisor of two natural non-negative numbers, using only subtraction.
  2. Write a program that determines the greatest common divisor of two natural non-negative numbers, using Euclid's algorithm.
  3. Write a program that determines the value of x raised to power n.
  4. Write a program that determines if a number is prime.
  5. Write a program that determines the sum of the digits for a number.
  6. Write a program that determines the first n terms of the Fibonacci sequence.
  7. Write a program that determines all the numbers smaller than n and are co-prime.
  8. Considering the following natural number sequence: $ x_n = x^2 - 79 \cdot n + 1601 $, determine the percentage of the values for n in a given interval (from a to b), so that the value $x_n$ is prime.

  9. Write a program that determines all the perfect natural numbers that don't exceed a given value n.
  10. Write a program that determines all the sequences of successive odd numbers, whose sum is n raised to the power 3. Ex. $ 1 = 1^3; 3 + 5 = 2^3; 7 + 9 + 11 = 3^3; $.

  11. Write a program that decomposes a given number n into its prime factors.
  12. Write a program that determines the decomposition of a natural even number, greater than 2, as a sum of two prime numbers.

9.4. Economic problems

  1. Considering the evolution of a bank deposit that is reconstituted each month, write a program that takes as inputs the initial deposited sum, the annual interest, and the number of months, and computes the final value of the deposit.
  2. In the context of the previous problem, and by taking into consideration also the annual inflation (that we consider a constant given as an input), and applying this inflation at the end of each 12 months, compute the net value at the end of the period.