Notes - 3 - Basics - of - Python - Jupyter Notebook
Notes - 3 - Basics - of - Python - Jupyter Notebook
1. Python is an interpreted, general-purpose programming language and supports procedural, functional and
object-oriented paradigms.
2. It's core philosophy is that the code is beautiful, explicit, simple, and readable.
3. To promote this core philosophy, as users you need to keep certain things in mind. Your code should be
simple, readable and well-documented.
4. Some key elements with regards to this course. It is important that your code works in the simplest possible
manner. You can use fancy optimization and clever tricks later. Always, use a function whenever possible
while writing a code, so that it can be reused.
5. Jupyter notebook (nb) is an interactive computing platform that spun out of IPython (Interactive Python).
But note that commands that require user input such as read or cat > file.txt, may not be straightforward. So,
let us keep those only for the terminal.
If you are writing a bash script, then use the header %%bash before writing the script as shown in the second
example.
In [21]:
!pwd
!cat /home/hdhar/Dropbox/Trial/rhapsody.txt # you can create a file elsewhere and o
!ls
/home/hdhar/Dropbox/PH434_Autumn2022/Classes_Notebooks/Notebooks
Caught in a landslide,
In [54]:
%%bash
vartext='Physics'
varnum=2022
greet="This is the $vartext batch of $varnum"
echo $greet
echo ''
a=2
b=3
c=$((a+b))
echo $c
In [71]:
x = 14
print (x) # Print the variable x, which we have not declared yet
type(x) # The interpreter has dynamically assigned it the type integer (int)
14
Out[71]:
int
In [74]:
Shahrukh Khan
Out[74]:
str
In [76]:
name = 14
print(name)
print(type(name)) # The interpreter has now dynamically changed it to the type inte
Shahrukh Khan
<class 'str'>
14
<class 'int'>
In [102]:
print(text)
Hello, world!
How is life?
In [88]:
x = 100045666
print(x)
type(x)
100045666
Out[88]:
int
In [89]:
y = 25.3677
print(y)
type(y) # The interpreter has assigned it the type floating point (float)
25.3677
Out[89]:
float
In [92]:
x = 1.000456668787787098890 # Only 15 places are kept after the decimal with the
print(x)
type(x)
1.0004566687877872
Out[92]:
float
In [93]:
x = 100045666
y = 25.3677
z = x+y
print(z)
type(z) # The interpreter has now dynamically changed it to the type float
100045691.3677
Out[93]:
float
Python float values are represented as 64-bit double-precision values. The maximum value any floating-point
number can be is approx 1.8 x 10308. Any number equal to or greater than this will be indicated by the string
inf in Python.
In [95]:
print (1.79e308)
print (1.8e308)
1.79e+308
inf
In [97]:
(2+3j)
Out[97]:
complex
In [99]:
2.0
3.0
In [103]:
In [106]:
print(pet[0]) # You can access the elements in the tuple starting from index 0
print(pet[1])
dogs
cat
In [112]:
pet[0] = 'fish' # You can't change elements already assigned during definition
pet.append('fish') # You can't add or remove anything from tuple
---------------------------------------------------------------------
------
3 print(pet)
In [119]:
dogs
cat
In [129]:
pet[0] = 'fish' # You can change elements already assigned during definition
pet.append('fish') # You can add anything in a list
print(pet)
In [127]:
In [128]:
In [ ]: