Getting Started with Python#

With Python you can do pretty much anything, you can automate every task. Its simpler to learn and use than any other language.

Python also offers much more error checking than C, and, being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries. Because of its more general data types Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those languages.

Python allows you to split your program into modules that can be reused in other Python programs. It comes with a large collection of standard modules that you can use as the basis of your programs — or as examples to start learning to program in Python. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk. Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary.

The interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. It is also a handy desk calculator.

Python Applications Area#

Python is known for its general purpose nature that makes it applicable in almost each domain of software development. Python as a whole can be used in any sphere of development.

Here, we are specifing applications areas where python can be applied.

  1. Web Applications

    • We can use Python to develop web applications. It provides libraries to handle internet protocols such as HTML and XML, JSON, Email processing, request, beautifulSoup, Feedparser etc. It also provides Frameworks such as Django, Pyramid, Flask etc to design and delelop web based applications. Some important developments are: PythonWikiEngines, Pocoo, PythonBlogSoftware etc.

  2. AI & Machine Learning

    • Python has Prebuilt Libraries like Numpy for scientific computation, Scipy for advanced computing and Pybrain for machine learning (Python Machine Learning) making it one of the best languages For AI.

  3. Desktop GUI Applications

    • Python provides Tk GUI library to develop user interface in python based application. Some other useful toolkits wxWidgets, Kivy, pyqt that are useable on several platforms. The Kivy is popular for writing multitouch applications.

  4. Software Development

    • Python is helpful for software development process. It works as a support language and can be used for build control and management, testing etc.

  5. Scientific and Numeric

    • Python is popular and widely used in scientific and numeric computing. Some useful library and package are SciPy, Pandas, IPython etc. SciPy is group of packages of engineering, science and mathematics.

  6. Business Applications

    • Python is used to build Bussiness applications like ERP and e-commerce systems. Tryton is a high level application platform.

  7. Console Based Application

    • We can use Python to develop console based applications. For example: IPython.

  8. Audio or Video based Applications

    • Python is awesome to perform multiple tasks and can be used to develop multimedia applications. Some of real applications are: TimPlayer, cplay etc.

  9. 3D CAD Applications

    • To create CAD application Fandango is a real application which provides full features of CAD.

  10. Enterprise Applications

    • Python can be used to create applications which can be used within an Enterprise or an Organization. Some real time applications are: OpenErp, Tryton, Picalo etc.

  11. Applications for Images

    • Using Python several application can be developed for image. Applications developed are: VPython, Gogh, imgSeek etc.

  12. Games and 3D Graphics

    • PyGame, PyKyra are two frameworks for game-development with Python. Apart from these, we also get a variety of 3D-rendering libraries. If you’re one of those game-developers, you can check out PyWeek, a semi-annual game programming contest.

Python Installation#

To install python, go to the website: https://www.python.org/downloads/ and install latest version, depending on your OS.

** Make sure during installation, to check the checkbox to add the Python reference in Environment variable Path

Once installation is done, you can check the Python version by running command to check installation is successfull

python --version

To use python interactively, you can directly type python in command line and insrantly start , experiment by typing python

python

Using a code editor of your choice#

To write Python code or any code, you could use PyCharm or VSCode, in this tutorial I will use VSCode which has simple and intiuitive UI , you can download from https://code.visualstudio.com/

VSCode offers many features, you can also install many extensions including extensions required to run Jupyter notebooks

Using python in a jupyter notebook#

Jupyter notebook is an interactive way to write your code for experimentation purposes, This tutorial is also written in Jupyter Notebook. (Learn more about it https://jupyter.org/)

  • Jupyter notebook has file extension .ipynb (interactive python notebook)

  • You can write markdown content as well as write and execute Python code

Package manager and virtual environment#

  • To install third party python modules/packages, you can use pip as package manager, there are other ways to install python packages like poetry, conda etc…

Example:

pip install virtualenv 
  • It’s always recommended to isolate various development environments/projects from your machine by installing and working on a virtual environment. To isolate, you can create a virtual environment in python and install all the packages inside it , instead of directly installing them in your machine.

# 1. Install virtualenv package
pip install virtualenv 
# 2. Create virtual env
virtualenv venv
# 3. Activate
venv\Scripts\activate

Informal introduction to python: using python as calculator#

Open your command line and type Python

>python

Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 100 + 56
156
>>>

As you saw above, it can act as a calculator, you can run python in any command line directly (interactive mode), there is no need to compile the code. That is how simple python is.

You can also operate on strings and lists

>>> 'Hello' + ' World!'
'Hello World!'
>>>
>>> x=[1,2,3,4,5] 
>> x.append(8)
>> x
[1, 2, 3, 4, 5, 8]
>>

Using Python inside Jupyter notebook#

There are two ways to use jupyter notebooks

  1. Inside VSCode

  2. Running notebook from command line

If you are in VSCode, launch VSCode, add a folder and add a file with extension .ipynb, open that file , you will be asked to chose interpreter, select python installed on your virtual environment. You can change the interpreter by running Ctrl + Shift + P and select interpreter. You may also recieve a notification to install jupyter kernel, install that if asked.

To run jupyter notebook from command line,

  1. Install jupyter notebook package

pip install jupyter
  1. Create a folder structure where you will store all your progress and within that folder, run:

jupyter notebook

A new window would be launched in your browser, click on New to create a notebook and rename it for your reference

Notebooks in these course are designed to run interactively on google colab (also using binder), click on the rocket icon and select colab, to open the notebook for interactive execution in colab, you can do additional experimentation by adding new cells, and save the copy of notebooks locally.

Python Strings#

x="I love python"

# accessing character by character
x[0]
'I'
# accessing range of characters x[start_index: end_index]
x[:6]
'I love'
# accessing last 3 characters
x[-3:]
'hon'
# accessing all characters except last 5
x[:-5]
'I love p'

Python lists#

# Compound list
p_list=[1,2,3,4,'a',5.0]
print(p_list)
[1, 2, 3, 4, 'a', 5.0]
# Appending list
p_list.append("Python")
print(p_list)
[1, 2, 3, 4, 'a', 5.0, 'Python']

Check python datatypes#

print(type(10))                  # Int
print(type(3.14))                # Float
print(type(1 + 3j))              # Complex
print(type('Asabeneh'))          # String
print(type([1, 2, 3]))           # List
print(type({'name':'Asabeneh'})) # Dictionary
print(type({9.8, 3.14, 2.7}))    # Set
print(type((9.8, 3.14, 2.7)))    # Tuple
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'set'>
<class 'tuple'>
# Gives absolute values
abs(-10.7)
10.7
# Gives max value
max(1,2,8)
8

To get all the definition , you can call help method

help(repr)
Help on built-in function repr in module builtins:

repr(obj, /)
    Return the canonical string representation of the object.
    
    For many object types, including most builtins, eval(repr(obj)) == obj.
In the upcoming tutorials, we will deep dive into specific concepts of Python, hold on your breath and start your journey#