Skip to content
Introduction to Python - Course Notes by Chinmay Garg
Introduction to Python
1. Python Basics
- Conceived by Guido Van Rossum (https://en.wikipedia.org/wiki/Guido_van_Rossum).
- Python is open source (free to use)
- Very easy to build packages (, which is code that you can share with other people to solve specific problems) in Python.
- "Swiss army knife" of programming languages
Install Python - https://www.python.org/downloads/
- To install packages hassle-free install
pip install manager
-
Python Shell - place where you can type code & immediately see results
-
IPython Shell - Interactive Python, juiced up version of regular Python created by Fernando PĂ©rez (https://en.wikipedia.org/wiki/Fernando_P%C3%A9rez_(software_developer)) and is part of the broader Jupyter (https://jupyter.org/)
- Python Script - text files with the extension
.py
. It's a list of Python commands that are executed, almost as if you where typing the commands in the shell yourself, line by line. - Putting your code in Python scripts instead of manually retyping every step interactively will help you to keep structure and avoid retyping everything over and over again.To make a change,simply make the change in the script, and rerun the entire thing.
- Add comments (
#
) to Python scripts - important to make sure that you and others can understand what your code is about.
# Addition
print(5+6)
Variable & Types
- Variable - "Save" values while coding to later call up by typing the name. Make your code reproducible.
- Variables have a specific type (
int, float, str
). Usetype()
. - A "sting" (
str
) is Python's and most programming languages way to represent text. Both''
and""
can be uset to represent strings. - Booleans - very useful Ex. perform filtering operations (
True
,False
)
a = 4 # (int)
b = 3 # (int)
c = a/b #(float)
type(c)
d = 'ab'
e = 'cd'
f = d + e
print(f)
type(f)
g = True
h = False
i = g + h # (results in integer value)
j = g or h # (use "or" for boolean algebra output)
print(i)
print(type(i))
print(j)
print(type(j)) # (use print to print "ALL" outputs. Otherwise only the latest one executes.)
# Example of using variables to store file paths as strings
# Define file paths
input_file_path = '/path/to/input/file.txt'
output_file_path = '/path/to/output/file.txt'
# Check the types of the variables
type(input_file_path), type(output_file_path)
# Example without raw string
file_path_escaped = "C:\Users\Name\Documents\file.txt"
print(file_path_escaped)
# Example with raw string
file_path_raw = r"C:\Users\Name\Documents\file.txt"
print(file_path_raw)
# Example without raw string
file_path_escaped = "C:\\Users\\Name\\Documents\\file.txt"
print(file_path_escaped)
# Example with raw string
file_path_raw = r"C:\\Users\\Name\\Documents\\file.txt"
print(file_path_raw)