Python Basics

Nummerical Operations

We can do simple calculations with Python by entering a calculation directly into the Python console.

>>> 2 + 3
5
  1. We can compute multiplication and division in Python, using an asterisk to indicate multiplication and a forward slash to indicate division.

The parentheses determine which operations are performed first.

>>> 2 * (3 + 4)
14
  1. The minus sign indicates a negative number.
>>> -10
-10
  1. If you try to divide by zero, Python produces an error.
>>> 10/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
  1. Besides addition, subtraction, multiplication, and division, we can also use exponentiation in Python.

The ** operator in Python is used to raise the number on the left to the power of the exponent of the right.

>>> 2**3
8
  1. To calculate remainder, we use %
>>> 6 % 4
2

You can use the modulo function (%) with 10 to find the ones’ place of an integer.

>>> 2%10
2
  1. random.randint(a,b) will return an integer between a and b (inclusive).

For example, random.randint(5, 10) could return any integer between 5 and 10 including both 5 and 10. We need to import random to use this method.

import random
num = random.randint(5, 10)
print(num)
  1. In-place operators allow you to shorten code from x = x + 5 to x += 3. We can apply with other operators such as -, *, / and % as well.

These operators can be used on types other than numbers such as strings.

>>> x = "Hello"
>>> print(x)
Hello
>>> x += " World"
>>> print(x)
Hello World

Comment

In Python, comments start with a #. To add a multiline comment you could insert a # for each line.

Comment can:

  • Provide context
  • Help other people reading the code understand it faster
  • Ignore a line of code and test your program without it
# This is a comment

Indentation

Python uses indentation to indicate a block of code. Python will give you an error if you skip the indentation.

number = 10

if number > 2:
  print("Number is greater than two!")

Variables

A variable stores a value in Python program.

We define a variable using an equals sign (=).

Variables can be reassigned as many times as you want, in order to change their value.

>>> x = 5
>>> print(x)
5
>>> x = "Hello"
>>> print(x)
Hello

Note: If you try to reference a variable you haven’t assigned, the program will show error.

Rules for variable name

  • Must start with a letter (usually lowercase)
  • After first letter, can use letters/ numbers/ underscores
  • So spaces or special characters
  • Case sensitive (my_var is different from MY_VAR)

Print

print() function displays the string value on the screen. The message to be printed should be surrounded by quotes.

If no arguments are provided, the print() function will output a blank line.

print("Hello World")

input()

To get input from the user in Python, we use input function.

The function prompts the user for input, and returns what they enter as a string.

>>> input("Enter your name: ")
Enter your name: Jane
'Jane'

len()

len() function returns the number of characters in the string

Errors

Python point to the location of error with a ^ character.

  • SyntaxError: something wrong with the way your program is written
  • NameError: detects a variable that is unknown

Import Python Modules

Modules (sometimes called packages or libraries) help group together related sets of tools in Python.

  • Import a Module: import pandas
  • Import a module with an alias: import pandas as pd