Python Programming Language Tutorial

NOTE: If you want a more interactive experience, please use this setup. It puts the Python interpreter in a different window below the tutorial. In this article, the links open the Python interpreter in a different tab. The choice is yours. 

The purpose of this article is to provide a basic Python programming language tutorial for beginners. We will explore the features of Python and also explore some of the Python syntaxes. Some Python language syntax we will explore is a print function, variables, simple data types, functions, and input and output.

Getting Started

An overview of Python and programming languages can be found here. The Python programming language is an interpreted high-level general-purpose scripting language. It was designed and released by Guido Van Rossum in 1991. Python over the last several years ranks in the top 5 of all programming languages in the world which shows its popularity. Python is used in many contexts such as Web development, system administration automation, data science/analytics, artificial intelligence/machine learning, robotic process automation, and many others.

The first step in using Python is to download the latest version of the interpreter from the Python.org website for your operating system of choice. There are many ways you can run Python programs such as the command line, using an integrated development environment, or in the interpreter. For Part I and II of this tutorial, we will run simple Python scripts using the interpreter to just give you a flavor of the language. To really learn Python, we highly recommend you take a course in Python.

\"\"/

(Image: the python.org website)

Basic Program Flow

\"\"/


A basic computer program can be considered a system. A system accepts input from the user, processes the input, and provides an output. Sometimes the program will error which is the feedback that the program provides to the user.

Hello world!

Your first Python program will be the obligatory Hello World program. Let\’s look at what this program looks like. At the Python interpreter prompt >>>, type print(\”Hello World\”). The output prints on the second line. It is the simplest Python script you will run.

>>> print(\”Hello World\”)
Hello World

How does it work?

The print() statement is a function in Python which returns the string \”Hello World\” which gets printed to the screen.

Throughout this tutorial, you will be given access to an interactive Python shell for you to use. When learning a new language, it is always good to imitate first and then try something yourself. So, when you see the Try it Yourself, do the code we walked through and then do your own example.

Try it Yourself

  • Try the code example
  • Print your full name to the screen

Variables

\"\"/

A variable in Python holds a value. Think of a series of mailboxes in your local post office. Each box has a number. The mailbox contains the mail of the recipient. To access a mailbox, you go to the number that is assigned to you.

In Python, you give a name to a variable and you assign it a value such as the following:

>>> a=\”bob\”
>>>

So, a contains the value of a string called bob. To see the value of the variable, you would print it such as:

>>> print(a)
>>> bob

Variables can store numbers, strings, and another type of data. For this introductory tutorial, we will stick with numbers and strings. Numbers can be integers and real numbers.

Some rules for variables:

  • Starts with a letter or _
  • Cannot begin with a number
  • Contains alpha-numeric characters and underscores (A-Z, a-z, 0-9, and _ )
  • Are case-sensitive. For example, me, ME, and _ME are different variables.

Try it Yourself

  • Try the code example above
  • Try the following example:
    • Create a string variable called firstname and store your firstname. Then, print your firstname to the screen.

Data Types

In this tutorial, we are only concerned with strings (str), integers (int), and floating point numbers (float). You can use the type() function to return the type of data stored in a variable.

>>> b = 10
>>> print(type(b)
<class \’int\’=\”\”>
>>> c = 10.5
>>> print(type(c))
<class \’float\’=\”\”>
>>> a=\”bob\”
>>> print(type(a))
<class \’string\’=\”\”>

You can specify the data type the variable should be.

>>> b = int(12)
>>> c = float(10.5)
>>> a = str(\”bob\”)
>>> print(a,b,c)
bob 12 10.5

Try it Yourself

  • Try the code example above
  • Try the following example
    • Create three variables x, y, and z. x is an integer storing 12, y is a float storing 3.5, and z is a string storing \”mean\”. Print the contents of those variables

Functions

A function is a block of code that runs when called. An example of a function in Python and how you call a function is:

>>> def pr(message):
….         print(message)

>>> pr(\”Hello World\”)
Hello World

Blocks in Python always start with a: and the next line has to be indented with a tab otherwise you will get an error.

Try it Yourself

  • Try the code example above
  • Try the following example
    • Create a function called sum, pass it in a variable, and print the result to the screen

Input and Output

In our code examples, we have been using the print function which prints the results to the screen which is output. In Python, you can accept input from the user using the input function. We will introduce a new character +. The + is a concatenation function. It concatenates one string with another string to create a new string.

>>> lastname = input(\’Enter last name: \’)
Enter last name: Smith
>>> print(\’lastname = \’ + lastname)
lastname = Smith

Try it Yourself

  • Try the code example above
  • Try the following example
    • Ask for the user\’s age and print the age to the screen

Conclusion

In this tutorial, we investigated some of the basic syntax of the Python programming language. We explored input and output, variables, data types, and functions. Python is an interpreted programming language which means it runs the program at run-time. You also used the interactive Python shell to practice the code we introduced in this tutorial.

I hope you have enjoyed this article. If you would like to see more tutorials on Python, please let me know in the comments. Even now I can make this more useful would be helpful. Thanks.

Dr. M

Dr. M

Leave a Reply

Your email address will not be published. Required fields are marked *