Class 1 ‐ Introduction to Programming - Justin-Boyd/Python-Class GitHub Wiki

Introduction to Programming

Computer Instructions

  • A computer doesn’t understand code like humans do.
  • It understands basic instructions called machine code.
  • A computer’s CPU handles its instructions and calculations.
  • Machine code is a language that enables fast execution. It can be regarded as the lowest level source code.

What Is Programming?

  • Programs

    • Programs are lists of instructions the computer performs in sequential order.
    • They typically include mathematical operations.
  • Source Code

    • A program written in a high-level programming language
  • A programming language includes a set of instructions that yield various types of results.

What Is Python?

  • A high-level programming language
  • Easy to deploy and learn
  • Python’s design emphasizes code readability.
  • Python is not meant for low-level programming.

Python vs. Other Languages

  • Python

    • Easy to learn
    • Works in any environment
    • Many libraries
    • Large community
    • Many frameworks
  • C#

    • Hard to learn
    • Works in specific environments
    • .NET libraries
    • Large community
    • .NET framework
  • Java

    • Hard to learn
    • Works in any environment
    • Few libraries
    • Large community
    • Syntax is easy to learn Syntax is difficult to learn Syntax is difficult to learn
    • Many frameworks

Python Interpreter

  • Python is an interpreter language.
  • Reads instructions from top to bottom and left to right, with exceptions
  • Performs instruction validation
  • Results are returned per instruction, in sequential order.
  • The interpreter can be run from within CLI using the py command or installed in the IDE.

Compiler vs. Interpreter

  • Compiler Interpreter

    • It takes more time to compile; however, the code execution is faster.
    • The code is compiled for a specific operating system.
    • The source code is typically unavailable, and the compiled data can be read.
    • Once the binary is compiled, the source code is no longer needed.
  • Interpreter

    • Execution takes more time, and each instruction must be compiled in real time.
    • Code can be executed in any environment that includes the interpreter.
    • The source code is available and readable.
    • The code is needed each time the program is executed.

Python 2 vs. Python 3

  • Python 2

    • Python 2 code is considered legacy code.
    • Many older libraries built for Python 2 are not forward-compatible.
    • Strings are stored as ASCII characters by default.
    • Calculations are rounded to the nearest whole number.
    • Print “Hello”
  • Python 3

    • Python 3 replaced Python2 at the beginning of 2020.
    • Most new libraries are being developed strictly for use with Python 3.
    • Strings are stored as Unicode characters by default.
    • Calculations are not automatically rounded.
    • Print(“Hello”)

Python Types

  • CPython is the original implementation of Python.
  • Jython is an implementation of Python designed to run on the Java platform.
  • IronPython is an implementation of Python that can be integrated into the .NET framework.

Python Installation

Python Website

Python Website

  • Simple installation process
  • The website where you can download Python is Python.
  • Python 3 includes better support for development.

Installation Process

Installation Process

  • Python installation for Windows is the same as any other installation.
  • By default, Python 3 is installed in the AppData directory.
  • It is recommended to add Python to the path.

Python in CLI

Python in CLI

  • Running the Python CLI interpreter in Windows is done using the CMD, by adding Python to the path.
  • Running either py or python3 launches the interpreter.

Python IDE

Working with IDE

  • Integrated Development Environment
  • A program dedicated to specific software development
  • IDE is easier to use than CLI.
  • Includes many useful tools for software development
  • One of the most useful IDE programs is PyCharm, which is used for Python programming.

IDLE

  • Python’s Integrated Development and Learning Environment (IDLE)
  • IDLE has two main windows: one for Shell and the other for editing.
  • IDLE is a cross-platform program that works on Windows, UNIX, and macOS.

Pycharm

Pycharm

  • PyCharm was developed by JetBrains.
  • Free community version: Pycharm
  • Simple Windows installation, with some required configuration

Python Environment & PyCharm

New Project

New Project

  • During development, treat a program like a project.
  • Projects help organize files and create a dedicated environment.
  • Projects are created using the New Project option.

PyCharm Settings

PyCharm Settings

  • PyCharm visual themes can be extensively configured.
  • Most default settings can be left as is, but some require selection.

Interpreter Configuration

Interpreter Configuration

  • PyCharm allows selection of different interpreters for different projects.
  • Python installation should be automatically detected.
  • Configuration is done via Settings > Project Interpreter.

PyCharm Virtual Environment

PyCharm Virtual Environment

  • PyCharm allows virtual environment tools to create isolated projects.
  • The purpose of a virtual environment is to manage all project settings.
  • Virtual environment creation is relatively easy.

PyCharm Code Execution

PyCharm Code Execution

  • PyCharm has several ways to execute code.
  • After execution, the console will appear with the results and errors.
  • The console informs the developer if there are mistakes in the code.

Basic Syntax

Python Data Types

  • String
  • Integer
  • Float
  • Boolean

Python Syntax

#This is a comment
print("Hello World!")
#prints Hello World!
"""
This is how
a multi-line string
Comment looks.
"""
  • Hashtag (#) renders an entire line as a comment.
  • Three quotation marks (""") are used in the beginning and end of multi-line comments.
  • Three quotation marks can be used in print() to print multi-line strings.

Standard Syntax Principles

number = 1
number = number + 5
#add 5 to number
print("Hello World!")
  • Most programing languages are case-sensitive.
  • Statements must be fully written.
  • Case-sensitive means that x is not the same as X, and John is not john.

Code Handling

def main():
print("Main function")
print("Second line")
main()
class = 5
#this variable name cannot be used and will trigger an error
  • Whitespace: Invisible characters that make the code more readable
  • Keywords: Python’s reserved words and instructions
  • Indentation: A mechanism that associates a block of code with a condition, loop, or class