Introduction to Python

Introduction to Python

Python is literally eating the programming world. It is growing in popularity and usage in ways that are pretty much unprecedented in the history of computers.

Python excels in a wide variety of scenarios – Shell scripting, task automation, and Web development are just some basic examples.

Python is the language of choice for data analysis and machine learning, but it can also adapt to create games and work with embedded devices.

Most importantly, it's the language of choice for introductory computer science courses in universities all around the world.

Many students learn Python as their first programming language. Many are learning it right now, and many more will learn it in the future. And for many of them, Python will be the only programming language they need.

Thanks to this unique position, Python is likely going to grow even more in the future.

The language is simple, expressive, and it's quite straightforward.

The ecosystem is huge. There seems to be a library for everything you can imagine.

Python is a high-level programming language suitable for beginners thanks to its intuitive syntax, its huge community, and its vibrant ecosystem.

It is also appreciated by professionals across many different fields.

Technically speaking Python is an interpreted language that does not have an intermediate compilation phase like a compiled language, for example C or Java.

And like many interpreted languages, it is dynamically typed. This means that you do not have to indicate the types of the variables you use, and variables are not tied to a specific type.

This has pros and cons. In particular, you write programs faster, but on the other hand you have less help from the tools to prevent possible bugs. This means that you will find out about certain issues only by executing the program at runtime.

Python supports a wide variety of different programming paradigms, including procedural programming, object oriented programming, and functional programming. It's flexible enough to adapt to a lot of different needs.

Created in 1991 by Guido van Rossum, it's been rising in popularity - especially in the past 5 years, as this Google Trends infographic shows:


Starting with Python is very easy. All you need is to install the official package from python.org, for Windows, macOS or Linux, and you're ready to go.

If you are new to programming, in the following posts I will guide you to go from zero to becoming a Python programmer.

And even if you are currently a programmer who specializes in another language, Python is a language worth knowing because I think it's only going to keep growing from here.

Lower level languages like C++ and Rust might be great for expert programmers, but they're daunting to begin with, and they take a long time to master.

Python, on the other hand, is a programming language for everyone – students, people doing their day jobs with Excel, scientists, and more.

It's the language everyone interested in coding should learn first.

How to Install Python

Go to https://www.python.org, choose the Downloads menu, choose your operating system, and a panel with a link to download the official package will appear:

Make sure you follow the specific instructions for your operating system. On macOS you can find a detailed guide on https://flaviocopes.com/python-installation-macos/.

How to Run Python Programs

There are a few different ways to run Python programs.

In particular, there's a distinction between using interactive prompts, where you type Python code and it's immediately executed, and saving a Python program into a file and executing that.

Let's start with interactive prompts.

If you open your terminal and type python, you will see a screen like this:

This is the Python REPL (Read-Evaluate-Print-Loop).

Notice the >>> symbol, and the cursor after that. You can type any Python code here, and press the enter key to run it.

For example try defining a new variable using

name = "Flavio"
        

and then print its value, using print():

print(name)        
Note: in the REPL, you can also just type name, press the enter key and you'll get the value back. But in a program, you are not going to see any output if you do so - you need to use print() instead.

Any line of Python you write here is going to be executed immediately.

Type quit() to exit this Python REPL.

You can access the same interactive prompt using the IDLE application that's installed by Python automatically:

This might be more convenient for you because with the mouse you can move around and copy/paste more easily than with the terminal.

Those are the basics that come with Python by default. However I recommend that you install IPython, probably the best command line REPL application you can find.

Install it with

pip install ipython
        

Make sure the pip binaries are in your path, then run ipython:

ipython is another interface that lets you work with a Python REPL, and provides some nice features like syntax highlighting, code completion, and much more.

The second way to run a Python program is to write your Python program code into a file, for example program.py:

and then run it with python program.py:

Note that we save Python programs with the .py extension - that's a convention.

In this case the program is executed as a whole, not one line at a time. And that's typically how we run programs.

We use the REPL for quick prototyping and for learning.

On Linux and macOS, a Python program can also be transformed into a shell script, by prepending all its content with a special line that indicates which executable to use to run it.

On my system the Python executable is located in /usr/bin/python3, so I type #!/usr/bin/python3 in the first line:

Then I can set execution permission on the file:

chmod u+x program.py
        

and I can run the program with

./program.py        

This is especially useful when you write scripts that interact with the terminal.

We have many other ways to run Python programs.

One of them is using VS Code, and in particular the official Python extension from Microsoft:

After installing this extension you will have Python code autocompletion and error checking, automatic formatting and code linting with pylint, and some special commands, including:

Python: Start REPL to run the REPL in the integrated terminal:

Python: Run Python File in Terminal to run the current file in the terminal:

Python: Run Current File in Python Interactive Window:

and many more. Just open the command palette (View -> Command Palette, or Cmd-Shift-P) and type python to see all the Python-related commands:

Another way to easily run Python code is to use repl.it, a very nice website that provides a coding environment you can create and run your apps on, in any language, Python included:

Signup (it's free), then under "create a repl" click Python:

and you will be immediately shown an editor with a main.py file, ready to be filled with a lot of Python code:

Once you have some code, click "Run" to run it on the right side of the window:

I think repl.it is handy because:

  • you can easily share code just by sharing the link
  • multiple people can work on the same code
  • it can host long-running programs
  • you can install packages
  • it provides you a key-value database for more complex applications



要查看或添加评论,请登录

Rishav Raj的更多文章

  • The with Statement in Python

    The with Statement in Python

    The statement is very helpful to simplify working with exception handling. For example, when working with files, each…

    2 条评论
  • Variable Scope in Python

    Variable Scope in Python

    When you declare a variable, that variable is visible in parts of your program, depending on where you declare it. If…

  • Classes in Python

    Classes in Python

    In addition to using the Python-provided types, we can declare our own classes, and from classes we can instantiate…

  • Functions in Python

    Functions in Python

    A function lets us create a set of instructions that we can run when needed. Functions are essential in Python and in…

  • User Input in Python

    User Input in Python

    In a Python command line application you can display information to the user using the function: We can also accept…

  • Python Basics

    Python Basics

    Variables in Python We can create a new Python variable by assigning a value to a label, using the assignment operator.…

  • Can AI Replace UI/UX Designers: Understanding the Impact of Artificial Intelligence on the Industry

    Can AI Replace UI/UX Designers: Understanding the Impact of Artificial Intelligence on the Industry

    Artificial Intelligence (AI) has come a long way in recent years and has the potential to assist in many fields…

  • Make a simple calculator from python

    Make a simple calculator from python

    # Program make a simple calculator # This function adds two numbers def add(x, y): return x + y # This function…

社区洞察

其他会员也浏览了