Welcome to the first article on thecodepad. This article is first in a series of posts designed to teach Python programming to new developers. As we move forward, I make no assumptions about your programming background and aim to deliver a series of interactive posts to help you learn python from scratch and experiment with the code on the website itself.
You may ask “Why Python?”. Well, for four years in a row, developers have rated Python as the language they most want to learn. In Stack Overflow’s 2020 Developer Survey, Python is ranked among the top 5 in both the most loved and the most used languages – no other language holds this distinction! To top it all, python is consistently ranked as the most popular language by IEEE Spectrum’s Top Programming Languages app. This makes it all the more reason to pick up the language!
Python tutorials are everywhere! Why another one?
There are a number of books and MOOCs designed to help beginners learn python. However, despite my best efforts, I couldn’t find online sources that combine the engagement offered by MOOCs with the brevity offered by books. My aim would be to attempt a combination of both for readers who prefer their tutorials terse, brief, and engaging. Nevertheless, if you would like to follow other resources, here are a few good ones:
- O’Reilly’s Learning Python: A book that goes in depth to help absolute beginners learn Python, right from installing the language to creating graphical applications.
- Corey Schafer’s Python Programming Beginner Tutorials: A series of 25 YouTube videos that cover the nitty-gritty of python. Other playlists on his channel also teach plotting graphs, handling large amounts of data, etc.
- MIT Open Courseware’s Introduction to Computer Science and Programming in Python: This is an extensive course that covers some core concepts in Computer Science and teaches its students to program those concepts in Python. It also hosts weekly quizzes and exercises to help you gauge your expertise level. I particularly like the fact that this course teaches you how to think like a programmer as opposed to just teaching you the tools to program.
If you are still reading, great! Welcome to the series :-). Let’s get cracking and install python on your system while I introduce some basic terminologies along the way.
Note: If you’re on MacOS, you should download the pkg installer instead of the bash installer.
Once the installer is downloaded, go through its steps and accept all defaults – it should be a very seamless process. After the installation is complete, you need to open the terminal app for your OS:
Windows: Go to start menu and open “Anaconda Prompt”.
MacOS: Open the “Terminal” app on your Mac.
In the terminal window, type:
conda install -y ipython
This should download the ipython interpreter and complete your set up.
Now that your python setup is complete, running it is fairly straightforward. Open your terminal app and type
ipython. This should give you an output like this:
rajat:~: ipython Python 3.8.3 (default, Jul 2 2020, 11:26:31) Type 'copyright', 'credits' or 'license' for more information IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help. In :
In here, we will write python programs that you can freely experiment with!
Don’t worry if you were unable to install python properly! You can write python code online and see its output. Check out any one of the following online python editors:
The upcoming tutorials will feature interactive coding editors so that you can edit the code and experiment with it on thecodepad itself. However, an offline installation on your PC is always handy :-).
Let’s kickstart your python journey with some simple terminologies that are largely valid across all programming languages.
- Keywords: These are words that python understands. You use them to make the computer do your bidding. One simple example of this is
- Variables: These are storage locations for your data. For example,
x = 5would store the number 5 in the variable x, just like mathematics.
- Operators: They are mathematical or logical operations that act on data. For example,
- Data Types: This is the type of data that you store in the variable. For example,
5is an integer,
"Hello World!"is a string, and
7.2is a float (Numbers with decimals are called float).
To make sure you understand the terminologies above, run the code below (there’s a green run button right on top)! You can edit the code and freely experiment with it if you create an account with repl.it.
Rules for naming variables
- A variable shouldn’t be a keyword. You can’t do something like
print = 10.
- Variable names should start with either a letter (A-Z or a-z) or an underscore (_).
- Apart from the first letter, they can contain letters (A-Z and a-z), underscore (_), and numbers (0-9).
- They are case sensitive – Dog and dog are different variables.
Different Types of Operators in Python
- Arithmetic operators: These are plus (+), minus (-), divide (/), multiply (*), mod (%), and exponent (**).
- Relational Operators: These are greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), and equal to (==). The funny operator for “equal to” arises from the fact that
a = 1is used to store 1 in
a == 1is used to check if
a‘s value is 1.
- Logical Operators: These are
not. They are used to combine the results of relational operators.
Commenting in Python
If you want to stop python from running a line of code or need to annotate a section of the code with the description of what it is doing, use
#. Python ignores anything that occurs in a line after a hash.
Practice what I preach
With the set of rules you learnt in the previous section, you should be able to do some basic number manipulation with python. Let’s give it a shot!
That would be it for Chapter 1! You should go over this article and try to play around with different combinations of variables and operators. Once you feel comfortable, you can jump to Chapter 2 next Friday to learn python.
About the Author
I write code and chug tea while watching Netflix on my machine’s second screen. Currently trying to reverse engineer Iron Man’s discarded Mark 1 armour.