# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Scientific Computing with Python (Second Edition)
# ## Chapter 01

# *We start by importing all from Numpy. As explained in this chapter the examples are written assuming this import is initially done.*

from numpy import *

# ## 1.1 Some Installation and configuration instructions

# *No code in this book section*

# ## 1.2 Program and program flow
# ### 1.2.1 Comments

# This is a comment of the following statement
a = 3  # ... which might get a further comment here  

# ## 1.3 Basic Datatypes
# ### 1.3.1 Numbers

from numpy import *

2 ** (2 + 2) # 16

1j ** 2 # -1

1. + 3.0j

# ### 1.3.2 Strings

'valid string'

"string with double quotes"

"you shouldn't forget comments"

'these are double quotes: ".." '

"""This is
 a long,
 long string"""

# ### 1.3.3 Variables

# +
x = [3, 4] # a list object is created

y = x # this object now has two labels: x and y

del x # we delete one of the labels
del y # both labels are removed: the object is deleted

x = [3, 4] # a list object is created
print(x)
# -

# ### 1.3.4 Lists

# +
L1 = [5, 6]

L1[0] # 5
# -

L1[1] # 6

# *Uncomment the next line in order to see the error*

# +
# L1[2] # raises IndexErrorL1 = [5, 6]
# -

L1[0] # 5

L1[1] # 6

# *Uncomment the next line in order to see the error*

# +
# L1[2] # raises IndexError
# -

L2 = ['a', 1, [3, 4]]

L2[0] # 'a'

L2[2][0] # 3

L2[-1] # last element: [3,4]

L2[-2] # second to last: 1

print(list(range(5))) # returns [0, 1, 2, 3, 4]

len(['a', 1, 2, 34]) # returns 4

len(['a',[1,2]]) # returns 2

# +
L = ['a', 'b', 'c']

L[-1] # 'c'

# +
L.append('d')

L # L is now ['a', 'b', 'c', 'd']
# -

L[-1] # 'd'

# ### 1.3.5 Operations on Lists

L1 = [1, 2]
L2 = [3, 4]
L = L1 + L2 # [1, 2, 3, 4]
L

L = [1, 2]
3 * L # [1, 2, 1, 2, 1, 2]

# ### 1.3.6 Boolean Expressions

2 >= 4  # False

2 < 3 < 4 # True

2 < 3 and 3 < 2 # False

2 != 3 < 4 or False # True

2 <= 2 and 2 >= 2 # True

not 2 == 3 # True

not False or True and False # True!

# ## 1.4 Repeating statements with loops

L = [1, 2, 10]
for s in L:
    print(s * 2) # output: 2 4 20

my_list = [...] # define a list
for elt in my_list:
    ...   #do_something
    ...   #something_else
print("loop finished") # outside the for block

# ### 1.4.1 Repeating a task

n = 30
for iteration in range(n):
    ... # a statement here  gets executed n times

# ### 1.4.2 break and else

x_values=[0.5, 0.7, 1.2]
threshold = 0.75
for x in x_values:
    if x > threshold:
        break
    print(x)

x_values=[0.5, 0.7]
threshold = 0.75
for x in x_values:
    if x > threshold:
       break
else:
    print("all the x are below the threshold")

# ##  1.5 Conditional statements

x = 5
if x >= 0:
    print(x)
else:
    print(-x)


# ## 1.6 Encapsulating code with functions

def f(x):
    return 2*x + 1


f(2) # 5

f(1) # 3

# ## 1.7 Understanding Scripts and modules

z = []
for x in range(10):
    if f(x) > pi:
        z.append(x)
    else:
        z.append(-1)
print(z)

# ### 1.7.1 Simple modules - collecting functions

# *The next command requires that the file smartfunctions.py exists in the same folder as this file*

exec(open('smartfunctions.py').read())

# ### 1.7.2  Using modules and namespaces

# *The next commands require that the file smartfunctions.py exists in the same folder as this file*

import smartfunctions
print(smartfunctions.f(2))      # 5

from smartfunctions import g    #import just this function
print(g(1)) # 0

from smartfunctions import *    #import all
print(h(2)*f(2))                # 1.0


# ## 1.8 Python Interpreter

def f(x):
    return y**2  
a = 3   # here both a and f are defined

# *To see the runtime error you have to uncomment the next command*

# +
# f(2) # error, y is not defined
# -

# ## 1.9 Summary

# *No code in this book section*

StarcoderdataJupyterScriptsDedupFiltered