Lecture 5. Introduction To Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Agenda

 What is programming?

 Programming languages

 Why do we need to learn Python?

 Language translations

 Introduction to Python

2 2024-04-02
5.1 What is programming?
 The process of writing computer programs

 A complete program is a list of instructions that require computers to execute


 Task-oriented program

 As simple as adding up a bunch of numbers

 As complicated as your operating system

3 2024-04-02
5.2 Programming languages
 A specific way of telling a computer how to do what you want

 A programming language is a language that


 Has grammar rules
 Keywords

 Programming languages:
C
 C++
 Java
 Visual Basic
 Python
…

4 2024-04-02
5.2 Programming languages
 Markup languages: HTML, XML, SGML, XHTML
 Programming languages versus Markup languages
 Markup languages are used to create documents
 Programming languages are used to create programs
 Markup languages describe documents
 Programming languages provide instructions for computers to execute

 Programming languages allow programmers to add extra functions while markup languages do
not support.
 Programing languages support the use of variables, conditional statements, and iterative
statements.
5 2024-04-02
5.3 Why do we need to learn Python?
 Low-level languages (assembly languages/machine languages) are used to perform
specialized tasks like embedded items, microcontrollers.
 Well-known languages such as C, C++, Visual Basic, Java are
 Used to create desktop applications
 Not suitable for the Web

 Python is a high-level language


 Python is able run on most types of computers regardless operating systems installed on computers
 A human-readable language

 High-level languages are converted into low-level languages to be executed


6 2024-04-02
5.3 Why do we need to learn Python?
 Python can do
 Web Development frameworks: Django, Flask, or Pyramid

 Data Analysis and Visualization Libraries: Numpy, pandas, and Matplotlib

 Machine Learning and AI Libraries : TensorFlow, PyTorch, scikit-learn

 Game Development Libraries : Pygame, Panda3D, and Godot

 Desktop GUI Application Libraries : Tkinker, PyQt, and wxPython

 Scientific Computing Libraries : SciPy, SymPy, Astropy

7 2024-04-02
5.4 Language translations
 Programming language translators
 Assembler: translate assembly language to machine language
 Compiler
 Interpreter

 The compiler and interpreter convert all of the source code of a high-level language into
machine language
 Interpreter: Each piece of code is interpreted and then executed in order, and if an error is
discovered inside one part of the code, it stops the code from being interpreted without
being translated to the next set of codes.

8 2024-04-02
5.5 Introduction to Python
 Python is a popular, object-oriented, and structured programming language

 Python is highly extensible: Packages for Python can be imported to obtain additional
functionality through conda or pip. Example

 A Python program is just a text file that you can edit directly

 Some editors for programming


 Sublime
 Atom
 Notepad++

9 2024-04-02
5.5 Introduction to Python
 Whitespace indentation of a piece of code affects its meaning

 A logical block of statements must have same indentation

 Avoid using TABs because they greatly complicate the indentation scheme

 Link: https://developers.google.com/edu/python/introduction

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

https://docs.anaconda.com/free/navigator/install/

https://www.youtube.com/watch?v=eWRfhZUzrAc&ab_channel=freeCodeCamp.org

10 2024-04-02
How to run a python program on Windows
 Step 1: Download Python from official Python website Python Releases for Windows |
Python.org

 Step 2: Install Python on your PC

 Step 3: Using Windows PowerShell to run


a python program using the following syntax:
python python_program_name.py

 Notepad++ can be used to create a python program

11 2024-04-02
Example
 Find solutions of a quadratic equation
#Find solutions of a second degree equation #User-defined function
import math def greeting(name):
a = float(input("a = ")) print("Hello",name)
b = float(input("b = ")) #Call the function
c = float(input("c = ")) var_name = input("Enter your name:")
delta = b**2 - 4*a*c greeting(var_name)
If delta > 0:
x1 = (-b + math.sqrt(delta))/2/a
x2 = (-b - math.sqrt(delta))/2/a
print("The given equation has two distinct real solutions:")
print("x1 =",x1)
print("x2 =",x2)
elif delta<0:
x1 = complex(-b/2/a, math.sqrt(-delta)/2/a)
x2 = complex(-b/2/a, -math.sqrt(-delta)/2/a)
print("The given equation has two distinct complex solutions:")
print("x1 =",x1)
print("x2 =",x2)
else:
x = -b/2/a
print("The given equation has a unique solution x =",x)

12 2024-04-02

You might also like