Introduction to python
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
The intention here is to teach the basics of python programming language in a beginner friendly way. So, by no means will these tutorials be comprehensive. But I do hope these basics will help readers understand the fact that we want to use python as a tool for machine learning and not as a programming language.
Contents
Data Types
- Integer: 1, 2, 10
- Decimal numbers: float: 1.0, 2.1, 10.5
- Boolean: True, False
- Nonetype: Null values
- String: "It is some character sequence"
Typecast
- Implicit type casting
- Explicit type casting
Operators
- Arithmetic
- Assignment
- Comparison
- Logical
- Identity
- Membership
- Bitwise
Data Structures
- Lists
- Tuples
- Dictionaries
- Set
- Chained Sequences: lists, tuples, chained dictionaries
- Indexing
Conditional statements
- Code block
- if
- if else
- if elif else
Loops
- for loop
- while loop
- nested loops
- nested loops with conditional statements
Functions
- Inbuilt functions
- Custom functions
- return keyword
- Arguments: positional, keyword, kwargs
- Parameters: Default
- Tuple packing / unpacking
- return vs yield keyword
- Anonymous functions: lambda functions
- Scope: local environment vs global environment
- Generators
Debugging
Classes, methods and objects
Exception Handling
User Input
String Formatting
File Handling
External Libraries
- re
- os
- Numpy
- Pandas
- Scipy
- Matplotlib
- Seaborn
- Plotly
- Scikit-Learn
- NLTK
- Opencv
- Tensorflow
Programming language, no matter which one, is just a tool to complete some sort of objective. If our objective is to perform some mathematical operations, we could choose a programming language as a calculator. There exists multiple programming languages.
Python is just of these programming languages. One could argue that one language is better than the others, but its about our end objective that really defines which programming language we should choose.
There's a saying in programming community, "If all you have is a hammer, all you would ever see is a nail". And for that reason, as we learn and grow, we eventually practice more than one programming language.
So, there exists multiple programming language to solve different types of problems. If our objective is to create a web page, we could use python, c# etc. There are dedicated softwares using which we can create websites without even programming at all.
Its just that a lot of people use python now and if we learn the same language as they do, it helps us to communicate with them therefore making our learning process easier.
Well, there are some minor differences between different programming languages (like the syntax), but the logic is the same between them. They all have the concept of data types, variables, functions, conditionals, and so on.
If at this point you've heard about AI (artificial intelligence) then python is a good starting point because a lot of experts have made a lot of functionalities for python. Its not like, these functionalities couldn't be developed for other programming languages, its just that, if majority speaks english, then it makes sense to write books in english so that the particular book could reach to the masses.
Jupyter Notebook Basics
- Text cells
- Code cells
- Command Mode
- Edit Mode
- Shorcuts
Google colab uses ipy kernal, which is almost like a python engine, except that it gives a little more use-ablity than the standard python kernal.
Text / Markdown Cells
In Jupyter Notebooks, we can also create text cells. These cells are used for writing notes, explanations, headings, and other content alongside our code.
Python does not execute anything inside a text cell. Instead, the text is displayed as formatted content.
Text cells use Markdown, a simple syntax for formatting text. They also support LaTeX, which is commonly used to write mathematical notation and equations.
For example, we can write a mathematical expression like:
$$ \text{Mean Squared Error} $$
Or a more complicated equation:
$$ \text{error}(\theta0,\theta1) =
\frac{1}{2m} \sum{i=0}^{m-1} \left( (h{\theta0} + h{\theta1}x1^{(i)}) - y^{(i)} \right)^2 $$
This means we can keep our code, explanations, mathematical equations, and even images together in the same Colab notebook.
That makes notebooks especially useful for learning and experimenting, because we can explain what our code is doing right next to the code itself.
We can even add images to our Markdown cells when they help explain an idea.
Code Cells
Code cells are where we write our python code. Simple enough, right.
Cell execution: Press the run/play button located on the leftmost side of the cell
Keyboard shortcuts
Important ipynb notebook (colab) shortcuts
shift (hold) + enter (press): To execute a cell, hold the shift key on your keyboard and press enter to execute a cell. The cell will be executed and colab will take the user to the cell below. If the next cell doesn't exist, colab will create one.
ctrl (hold) + enter (press): Execute the current cell and stay on it
alt (hold) + enter (press): Execute current cell, create a cell below and take the user to the cell below.
Command mode
If you are writing something inside a colab cell, press esc key to get into command mode. From there, you could use following keyboard shortcuts.
- To add cell above: press a
- To add cell below: press b
- To delete current cell: d,d Press d two times in succession
- To undo / retrieve deleted cell: z,z
Specific to ipy kernal of jupyter notebook (not for colab)
- Change cell type to markdown: m
- Change cell type to code: y
- Change cell type to raw text: r
Edit mode
If you're typing in a cell, you're in edit mode. If you're in command mode, press enter on a cell to get into the edit more.
Python basics
Programming language as a calculator
10 + 21210 * 22010 / 25.010 - 28square
10 ** 2 100cubic power of 10
10 ** 3 1000- Python ignores whitespace between different objects in same line
- Python uses new lines to complete a command
- Python uses indentation to separate a certain code block from the rest of the code.
We'll talk about code block and objects later as well.
Variables
We could take any integer or number and give it a name. That data will be stored as a name and that name would be remembered by python.
Let's take an integer 10 and assign it to the variable a
a = 10New ability unlocked: print() function
Use: Print to console
print() function takes some input and outputs it into the python console. We will study functions in detail later on.
We will now print the value assigned to a.
print(a) 10
b = 2print(b)2
print(a + b)12
print(a - b)8
Comments
Use: Python will ignore every word written after the # sign
# Comments: useful to make notes
# The words that I'm writing here (after the # symbol)
# in the code cells are ignored by python
print(10)
# print(20) # will not be printed in ouput as we are using the # symbol10
Best practices for the variable names
- Variable name must start with a letter
- Variable name should not start with a number
- Variable names should only contain alpha-numerics or underscores (A-Z, a-z, 0-9, _)
# We can choose any variable name but we have to watch out for some certain rules.
# Variable name must start with a letter
variable1 = 10 # is allowed
print(variable1)
# Variable name should not start with a number
# 10variable1 = 11 # is not allowed
# Variable names should only contain alpha-numerics or underscores (A-Z, a-z, 0-9, _)
our_first_variable = 12 # is Allowed
print(our_first_variable) 10
12
Its generally a good practice to separate code chunks into different code cells for learning purpose. For final development purpose, we usually should copy all the code from different cells into a different python script (or a cell at the bottom) to ensure if everything in the script is working fine.
a = 10
print(a)
# Whitespaces between different objects are ignored by python
# We'll talk about objects later as well
b= 2 # Notice: I'm intentionally using whitespaces irregularly here
print(b)
print(a+b)
print(a -b)10
2
12
8
ipy kernal in notebooks (Colab) have an autoprint feature which autoprints the variable located at the most bottom code line
a10a
b2a
b
a + b # value of a+b will get auto-printed by ipy notebooks (colab)
12print(a)
print(b)
print(a+b)10
2
12
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →