Basics on python and coding
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).
1. Bash commands on Jupyter nb
You can write most of bash commands in Jupyter nb and execute them. For some functions, you may need to
append the command with an ! sign.
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
Is this the real life?
Is this just fantasy?
Caught in a landslide,
No escape from reality.
Check Notebook_3_Basics_of_Python.ipynb trial.txt
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
This is the Physics batch of 2022
2. Numbers and string in Python - Dynamical type
As mentioned Python is a dynamically typed language. This implies that the type of the variables (integer, string
etc.) do not need to be declared at the start, but are dynamically assigned while the code is interpreted.
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]:
name = 'Shahrukh Khan'
print(name)
type(name) # The interpreter has assigned it the type string (str)
Shahrukh Khan
Out[74]:
str
In [76]:
name = 'Shahrukh Khan'
print(name)
print(type(name)) # The interpreter has assigned it the type string (str)
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]:
text = '''Hello, world!
How is life?
When will this class end!''' # You can define a multiline text as a string
print(text)
Hello, world!
How is life?
When will this class end!
3. Integers, floats and complex number
A number in python can be stored either as an integer, a floating point (decimals) or a complex number
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]:
x = 2 + 3j # Defining complex numbers
print(x)
type (x) # The interpreter has dynamically assigned it the type complex number (co
(2+3j)
Out[97]:
complex
In [99]:
print(x.real) # use or print real part
print(x.imag) # use or print imaginary part
2.0
3.0
4. Tuples and list
Tuples and list are used to store multiple items in a variable
In [103]:
pet = ('dogs','cat') # This is a tuple
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
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
Input In [112], in <cell line: 1>()
----> 1 pet[0] = 'fish' # You can't change elements already assigned
during definition
2 pet.append('fish') # You can't add or remove anything from tu
ple
3 print(pet)
TypeError: 'tuple' object does not support item assignment
In [119]:
pet = ['dogs','cat'] # This is a list
print(pet[0]) # You can access the elements in the list starting from index 0
print(pet[1])
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)
['fish', 'cat', 'fish', 'fish', 'fish', 'dog', 'fish']
In [127]:
var = [3,4,'cat','fish'] # You can store different types in a list
print (var)
[3, 4, 'cat', 'fish']
In [128]:
var.append('dog') # You can add or remove anything from tuple
print(var)
[3, 4, 'cat', 'fish', 'dog']
In [ ]: