Lecture 5. Introduction To Python
Lecture 5. Introduction To Python
Lecture 5. Introduction To Python
What is programming?
Programming languages
Language translations
Introduction to Python
2 2024-04-02
5.1 What is programming?
The process of writing computer programs
3 2024-04-02
5.2 Programming languages
A specific way of telling a computer how to do what you want
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
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
9 2024-04-02
5.5 Introduction to Python
Whitespace indentation of a piece of code affects its meaning
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
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