Path: blob/master/material/basic-scripting.ipynb
934 views
Basic Scripting with Python
"Regular coding", as some compiled languages like C and Fortran, is also possible in Python, where everything is made explicitly. Although regular coding is less abstract and more clear, in most cases it is also less efficient. Fortunately, features of Python like operators overloading, easy import of auxiliar libraries and the OOP (Object-Oriented Programming) paradigm make Python very efficient and saving when coding.
The goal of this session is to learn several tools offered by Python and auxiliar libraries for basic tasks like data manipulation, evaluating functions, argument passing, search methods, etc.
For the proposed activities:
Do not hesitate to take your first guess when writing your own codes! (by now)
Do not try other ways more complex or less natural for YOU yet!
It is also important that you work alone, so you can contrast later YOUR way with the alternative offered by Python!
Example 1
Write a program that calculates the sum of N numbers
Example 2
Write a program that generates a grid for the function:
in the range with a step of .
Miscellanea
Solve the next exercises:
1. Write a program that sort a list of N numbers.
2. Find all the elements of a list of N numbers that are greater than the mean value of them. Then, create a new list with those numbers.
3. Write a program that gives the name of students who passed a course (grade over ).
4. Write a program that evaluates the next expression for given values of , and :
you may find useful the library math
for the functions and . Import it as import math
5. Find all the prime numbers between 1 and 100.
6. Write a program that calculates the factorial of a given number.
7. Write a program that evaluates the function by using the Taylor expansion using , and terms.
$$\cos(x) = \sum_{n=0}^\infty \frac{(-1)^n}{(2n!)}x^{2n}\ \ \$$8. Write a program that calculates by using the next approximations:
and
Which approximation takes less terms for achieving a good accuracy?
In Python everything is an object with methods and attributes.