Learn Python in 7 days: Day 1 - Getting started

Shashank Shetty
Shashank Shetty

Comment section

You need to be a subscriber to comment


Getting started

List of contents:

  1. Introduction
  2. Setting up a Python environment
  3. Running your first program
  4. More to know

Introduction

Learning to code is a must-learn skill today; even if it is irrelevant to our work. When we are surrounded by computers from all four directions, it only makes sense to have at least some knowledge about programming them. That's why I have decided to start this simple series. Firstly, the title is a tiny bit misleading as "Learning Python" in 7 days is damn near impossible. You will however have foundational understanding of the language and be able to write simple programs on your own after this. This is an introduction to the most important concepts of Python to spark interest and create a springboard for further learning.

Why Python? Well, that's the language I know; and for beginners it is inarguably the easiest language to learn. Not only that, it is also one of the most versatile languages out there and has wide real-life applications as well as career opportunities. Even though it can be used for everything from front-end web and app development to hardware control and IoT, it is professionally used for complex mathematical calculations and simulations, bulk data processing, machine learning and back-end maintenance such as managing databases.

Python is what is called an interpreted language, which is different from compiled languages (such as C) in the sense that a Python code is converted into machine code (basically binary code that the processor can understand) line-by-line by an interpreter and then executed. An entire source code of a compiled language on the the other hand is turned into machine code and then executed. I don't know much about it; you can dive in deeper if you wish. What I am concerned about is just being able to get stuff done. Importantly, you have to do more research about each topic from the Internet (W3 schools is great) or the official Python documentation. Not only that, you need to run all the code snippets given and experiment with your newly gained knowledge. So let's begin without further ado!

Setting up a Python environment

Firstly, you'll need to be able to run your code. It is preferable that you have a computer or a laptop; whether it runs Windows, MacOS or Linux (hats off mate!). You have to install Python on your computer and run the code in a terminal or command prompt. You can write this code in a text editor or an IDE (such as PyCharm by JetBrains)

Honestly, an IDE is overkill, but you can use it if you think it will be worth it and you are very serious about programming. The simpler way, which I will use throughout this course, is to use a text editor with a Python installation. If you are using Linux, Python is pre-installed on your system. If using Windows or MacOS, you can visit Python's official website and download the appropriate Python variant. At the time of writing this, the latest version is 3.13.3. Note that Windows 7 supports only till 3.8. Python 3.x should be fine for the basic stuff, but make sure to not get Python 2, the legacy version. Run the Python installer. The default settings should be good enough; but if you know what you are doing, you can manually configure the installation. Just ensure that Python is added to PATH and that pip and tkinter are installed, as it can be hastle to install them later.

Once you have installed Python, you can check if it is working by opening a terminal or command prompt (Windows + R and then 'cmd') and typing python --version. If it shows the version number, you are good to go. If not, Python is probably not added to the PATH. You can Google fixes for it.

If you do not have a PC, no need to be disappointed as there is a workaround. If you have an Android device, install PyDroid from the Play store; if you have an iPhone, install Python programming interpreter from the App store, or any other similar apps. They should do the job for simply learning the basics. Check out this video to learn to use PyDroid3. There is a third option which you can use on both PCs and mobile phones, online interpreters. This is not preferable, but it does work. Online-Python, Python Online and Programiz are good ones.

Running your first program

THAT'S IT! You now have Python up and running on your system. Start Python by typing python into the command prompt and type print("Hello World!"). Congratulations! You just ran your first piece of Python code. Note that you can type quit() to exit Python.

You just used REPL, an interactive way to run Python in which a single line is entered by the user and is executed by the interpreter using previous context. Typing code line-by-line is not feasible and that's not how programming is done. You write your entire program in a single file with the .py extension and run it from the terminal. A text editor is used for this purpose. I prefer VS Code, which is as close as it gets to an IDE without being an IDE. But you can use any text editor. Sublime text, Atom, Notepad++, Vim and Thonny (a very simple IDE) are the popular ones. But you can just use the in-built Notepad app in Windows if you want to, just ensure th file extension is .py and not .txt.

Create a new file in VS Code by typing Ctrl + N. Enter the following code in the file and save it as hello.py, you can name it anything


print("Hello World!")           
        

At the bottom left, make sure Python is recognised as the language mode and the proper Python interpreter is selected to run the program. Click on the run button in the top-right (a small triangle/arrow) and the terminal should print out "Hello World!".

More to know:

  • You can also run the file from the terminal or command prompt by going to the file directory and typing python hello.py.
  • Type Ctrl + K followed by Ctrl + S in VS Code and look for 'Python: Run python file' and set up a keyboard shortcut for it (like Ctrl + R) to easily run your code. You can enter into a sophisticated Python REPL in VS Code by typing Ctrl + Shift + P and looking for 'Python: Start Native Python REPL'
  • Comments are pieces of text that written after a hash symbol (#) and are not considered for execution by the interpreter. They are used to clarify context or provide more information about something in the code.
  • PIP is the default package manager for Python which is used to install packages to extend the functionality of Python. Installing with pip is very simple, e.g., pip install numpy
  • A virtual environment is often used for individual projects. It is like a standalone environment which has its own copy of Python and other packages. A different version of Python than the global version can be used within a virtual environemnt. It is not necessary, yet recommended to avoid dependency clashes and to have a safe disposable environment. Install it using pip with pip install virtualenv. Create a new virtual environment in the target directory by typing python -m venv env_name where 'env_name' is the name of the virtual environemnt. Use env_name/Scripts/activate to activate and deactivate to deactivate the venv in Windows Command prompt.
  • You can also run Python code in a Jupyter notebook. Jupyter is a web-based interactive environment for running Python code. It is not necessary for this series, but it is a good tool to know about. You can install it using pip by typing pip install jupyter in the terminal. Once installed, you can start it by typing jupyter notebook in the terminal. This will open a new tab in your browser with the Jupyter interface. You can create a new notebook by clicking on the "New" button and selecting "Python 3". You can then enter your code in the cells and run them by pressing Shift + Enter.
  • Modules are used in Python to add utilities to our program. Modules are basically Python files that provide additonal functions and classes. You can create your own modules as well. The syntax import module_name at the beginning of the file is used to import a module. Here is an example involving such modules. Notice the comments, string conversions, escape characters and text concatenations:

import datetime, math #Multiple imports in one line
from time import sleep #Importing specific function from a module
            
#The datetime module provides classes for acessing and manipulating dates and times
print("The current time is " + str(datetime.datetime.now()) + "\n\n\n")
            
#The time module provides various time-related functions, such as sleep
print("Countdown begins:-")
print("3")
sleep(1) #Pause the program for 1 second
print("2")
sleep(1)
print("1")
sleep(1)
print("Time's up! \n\n\n")
            
#The math module provides mathematical functions and constants
print("The value of pi is " + str(math.pi))
print("The square root of 49 is " + str(math.sqrt(25)))
print("The sine of 90 degrees is " + str(math.sin(math.radians(90))))