Python Programming Guide for Researchers - Part 1

Python Programming Guide for Researchers - Part 1

Basics of Python 3

Python has two different modes. These modes are called the interactive mode and the standard mode.

The interactive mode is meant for experimenting your code one line or one expression at a time. In contrast, the standard mode is ideal for running your programs from start to finish.

Objects

Objects whose value can change are said to be mutable objects, whereas objects whose value is unchangeable after they've been created are called immutable. Each object in Python has three characteristics.

  • Object type

  • Object value

  • Object identity

Object type tells Python what kind of an object it's dealing with. A type could be a number, or a string, or a list, or something else. Object value is the data value that is contained by the object. This could be a specific number. Object identity as an identity number for the object. Each distinct object in the computer's memory will have its own identity number.

Most Python objects have either data or functions or both associated with them. These are known as attributes. The name of the attribute follows the name of the object. And these two are separated by a dot in between them.

The two types of attributes are called either data attributes or methods.

A data attribute is a value that is attached to a specific object. In contrast, a method is a function that is attached to an object. And typically a method performs some function or some operation on that object.

import numpy as np
# Definining arrays
x=np.array([1,3,5])
y=np.array([1,5,9])

# Calling mean method need parenthesis
x.mean()
y.mean()

# Calling shape attribute do not need parenthesis
x.shape
y.shape

Object type always determines the kind of operations that it supports. In other words, depending on the type of the object, different methods may be available to you as a programmer. Finally, an instance is one occurrence of an object.

Modules and methods

Python modules are libraries of code and you can import Python modules using the import statements.

# Import math module
import math

# Some functions from math module
math.pi
math.sqrt(100)
math.sin(10000)

# Choose one function from that module
from math import pi

Namespace is a container of names shared by objects that typically go together. And its intention is to prevent naming conflicts.

Now, the math module has a square root method, sqrt, but numpy also has a square root method, sqrt.

import math
import numpy as np

math.sqrt(100)
np.sqrt(100)

What is the difference between these two functions?

So far, it appears that these two functions are identical, but actually these two functions are quite separate and they exist in different namespaces. It turns out that the numpy square root function can do things that the math square root function doesn't know how to do.

Here's a simple example. What if I wanted to take the square root of not just one number, but several numbers, say, 2, 3, and 4? I can do this simultaneously for all of them. This is not possible using the math square root function.

# not possible in math function
np.sqrt([3,4,5])

What exactly happens when you run the Python import statement?

Three things happen. First, Python creates a new namespace for all the objects defined in the new module. Think of this as our new namespace. That's step one. Next, Python executes the code of the module within this newly created namespace. Finally, Python creates a name—let's say np for numpy—and this name points to the new namespace object. So, when you call the np.sqrt function, Python uses the sqrt function within the numpy namespace. Similarly, math has its own namespace with its own square root function. So, when you type math.sqrt, Python calls the sqrt function within the math namespace.

Numbers and basic calculations

And Python, in fact, provides three different numeric types. These are called integers, floating point numbers, and complex numbers.

# Addition
100+10 # => 110

#Multiplication
100*10 # => 1000

#Exponent
2**6 # => 64 

#Division
9/4 # => 2.25

#Rounding division
9//4 # => 2

underscore operator. And the value of the underscore operator is always the last object that Python has returned to you.

10/1.3
# => 7.692307692307692
_
# => 7.692307692307692

_*1.3
# => 10

Random choice

Python doesn't care about the nature of these objects.

import random

random.choice([34,45,54,38,99])
#=> At every run it will give random number from this set

random.choice(["AA","BB","CC"])
#=> We can use string as well

Expressions and Booleans

"==" checks if objects have the same value. "is" checks if objects are exactly the same.

[1,3] == [1,3]
# true

[1,3] is [1,3]
# false

Python has two modes: interactive for experimenting and standard for running full programs. Objects in Python can be mutable or immutable, each with a type, value, and identity. Attributes of objects can be data or methods. Python modules are libraries that can be imported, each with its own namespace to avoid naming conflicts. The `math` and `numpy` modules both have a `sqrt` function, but `numpy` can handle arrays. Python supports integers, floating-point, and complex numbers, and offers various arithmetic operations. The underscore operator holds the last returned value. The `random` module allows for random selection from lists. The `==` operator checks for value equality, while `is` checks for identity.