Introduction to Python

UR-Self-Service account (uregina.ca account)

What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is widely used to perform all kinds of computer tasks from web applications to processing financial data, including:

https://www.w3schools.com/python/python_intro.asp

When you study Python, quite often you will encounter a discussion on "compiled verses interpreted".

Wikipedia describes Python as an interpreted high-level programming language. https://en.wikipedia.org/wiki/Interpreter_(computing)

Compiler VS Interpreter

A compiler transforms high-level programs (source code) written by humans into the machine code that a (computer can directly execute) which are 0's and 1's in binary.

An interpreter is a program that simulates a computer that understands a high-level language.

The difference between interpreting and compiling is that compiling is a one-time translation; once a program is compiled, it may be run over and over again without further need for the compiler or the source code. In the interpreted situation, the interpreter and the source are needed every time the program runs. (John Zelle. Python Programming: An Introduction to Computer Science. 3rd Edition)

Is Python interpreted or compiled language? https://nedbatchelder.com/blog/201803/is_python_interpreted_or_compiled_yes.html

What do you need?

Replit

Replit is an online compiler, IDE, Editor, Interpreter and REPL for more than 50 programming languages. It is a zero setup for online Python programming. Replit

You need to apply for a free Replit account. How to sign up for a free Replit account

The video will guide you in applying for a Replit account with a meaningful username.

When creating your Replit account, it is important to choose a username that allows instructors to properly identify and mark your assignments. For example, my name is

I could make my Replit username to be: 88_casong16. If this one is taken by someone, try add different numbers at the end. Numbers could be random or meaningful,like birthdays, and etc.

Once you have a Replit account, you can start your first Python program.

Here are some videos on creating python programs on Replit.
Welcome to Replit.
Working with Python in Replit

Since Replit is an online Python IDE, access depends on a working internet connection and the website being operational. The free Replit account allows up to three active projects and a maximum usage of 10 hours per month.

Download Python as described in the next section:

Download and Install Python

Python Install

We focus on Python 3. Make sure to install Python 3, not Python 2. The latest version of Python can be downloaded at https://www.python.org/downloads/

PCs and Macs might have Python already installed. To check if you have python installed on a Windows PC or Mac.

Check to see if you have Python or not.

You can download Python for Windows, Linux/UNIX, Mac OS, and others. Download Python

2. You need a Python IDE. For example, Spyder, Atom, VS code, Pycharm, repl.it which is an online Python Interpreter (Compiler), and many more.

An IDE (Integrated Development Environment) normally has a source code editor, build automation tools, and a debugger.

Visual Studio Code VS. Pycharm

https://careerkarma.com/blog/pycharm-vs-vscode/

Other list of Python IDEs to choose from.

Python Syntax

Python Command Line

Python syntax can be executed by writing directly in the Command Line. For example, you can type the following lines in the Replit Python console, and you will see:

Python 3.8.12 (default, Feb 26 2022, 02:56:10) 
>print("Hello, CS students!")
Hello, CS students!
>print(2+2)
4
>print("2 + 2 = ",2+2)
2 + 2 =  4
>
  • > is a Python prompt indicating the interpreter is waiting for a command. A complete command is called a statement.

    There are three examples of print statement above.

    1. The first one asks Python to display the literal phrase Hello, CS students!
    2. The second one ask Python to print the sum of 2 and 2.
    3. The third combines both, prints 2 + 2 = in quotes, then the sum of two numbers.

    Try to type the following code in the Python Command Line:

    When you type the first line, then press Enter, the ... is automatically displayed at the beginning of the line, it means the statement is not finished, Python is waiting for more.

    >def main():
    ...  print("Hello, CS students!")
    ... 
    > main()
    Hello, CS students!
    > 
    
    • Keyword def is short for "define". It marks the start of the function header.
    • All the code between the def and end will be executed every time you call the function name.
    • The main() is to invoke (call) this function.
    • A colon (:) marks the end of the function header. It is required.

    Python IDE

    You can write Python programs in the Python IDE. A Python file has .py file extension, and running it in the Command Line.
    https://www.w3schools.com/python/python_syntax.asp

    Here is an example of creating a Python program called main.py

    
    # Student Name:
    # Student #:
    # Replit username:
    # Lab #/Asg#:
    # File: main.py
    def main():                           #6 Function name is called main. 
        print("Hello everyone!")          #7 Indented print() line to show this is part of the main function.  
        print("Have fun learning Python.")
    main()                                #9 The last line of the file is the command to invoke (call) this function.
    

    Compile (Build) and Run (Execute)

    To execute the main.py program using Replit, simply click on the Run button. However, if the program name is not main.py but rather hello.py, you will need to compile and run the program in the shell window. To do so, click on the Shell window tab located on the right of the console window. Then, type the following command in the Shell window:

    python filename.py
    
    Replace "filename" with the name of your program file, in this case, "hello.py". This will compile and run your program.

    What is the difference between Console and Shell

    https://askubuntu.com/questions/506510/what-is-the-difference-between-terminal-console-shell-and-command-line

    Python Indentation and Comments

    Indentation refers to the spaces at the beginning of a code line.

    Python uses indentation to indicate a block of code.

    Python uses a sequence of statements together to create a new command or function.

    https://www.w3schools.com/python/gloss_python_indentation.asp

    Comments starts with a #, and Python ignores them:

    https://www.w3schools.com/python/python_comments.asp

    Note: line 6, def main(): and line 9, main() are not required. Nonetheless, these two lines are included in all lab materials as a means of preparing students for the concept of functions in subsequent labs.

  • Review

    1. True or False

    1. An IDE does not include a source code editor.
    2. A colon (:) after the function name is required.
    3. Python uses indentation in coding to make code easy to read. Indentation is not necessary.
    4. Python ignores lines starts with # symbol.

    Practice

    You can use either an online Python IDE, such as Replit, or any desktop Python IDE, such as Spyder to write the following program.

    def main():    # function name is called main.     
        print("Python is a popular programming language.")
        print("25 + 25 = ", 25+25)
        print("3 * 67 = ", 3*67)
    
    main() # The last line of the file is the command to invoke (call) this function