Lab01 Python1
Lab01 Python1
We will use the Python language for all assignments in this course. In this Laboratory, we will
introduce basic features of Python via common calculations and algorithms.
1. What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in
1991. It is used for web development (server-side), software development, mathematics, system
scripting.
2. Python install
We can download Python for free from the following website (the latest version at this time is
3.10.8): https://www.python.org/downloads/
To check if you have python installed on:
- a Windows PC:
Search in the start bar for Python or run the following on the Command Line (cmd.exe),
and type:
- a Linux or Mac:
Open the command line (Linux) or the Terminal (Mac), and type:
3. Python Quickstart
Let's write our first Python file, called helloworld.py, which can be done in any text editor.
The content of the file “helloworld.py” is only one line:
5. Python Indentation
Indentation refers to the spaces at the beginning of a code line. The indentation in Python is very
important, and it indicate a block of code.
- Example:
- You have to use the same number of spaces in the same block of code, otherwise Python will
give you an error:
6. Python Comments
Comments can be used to explain Python code, make the code more readable, or prevent execution
when testing code.
- Comments starts with a #, and Python will ignore them:
7. Python Variables
- Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
- Variable Names
Rules for Python variables:
o A variable name must start with a letter or the underscore character
o A variable name cannot start with a number
o A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
o Variable names are case-sensitive (age, Age and AGE are three different variables)
o Pascal Case
Each word starts with a capital letter:
o Snake Case
Each word is separated by an underscore character:
You can get the data type of any object by using the type() function:
Float can also be scientific numbers with an "e" to indicate the power of 10.
You can convert from one type to another with the int(), float(), and complex() methods:
10.Python Booleans
Booleans represent one of two values: True or False.
11.Python Operators
- Python Arithmetic Operators
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
x=5
y=3
print(x + y)
print(5 + 3)
print(x * y)
x=2
y=5
print( x ** y ) #same as 2*2*2*2*2
print( x ** (1/2) )
print( (x + y) / (x - y) )
Trinh Hung Cuong - Applied Calculus for IT - 501031 7/15
Ton Duc Thang University
Faculty of Information Technology
- Python Assignment Operators
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
== Equal x == y
!= Not equal x != y
x=5
y=3
and Returns True if both statements are true x < 5 and x < 10
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
Exercise 1
Write a Python program to calculate the following expressions:
Exercise 2
Write a Python program to calculate the following expressions:
2
√2+√3 3
a. b. 53 + √5 + 55
√2×√3
13.Python Math
Python has a set of built-in math functions, including an extensive math module, that allows you to
perform mathematical tasks on numbers.
- The abs() function returns the absolute (positive) value of the specified number:
Math Methods:
Method Description
math.comb() Returns the number of ways to choose k items from n items without repetition and
order
math.copysign() Returns a float consisting of the value of the first parameter and the sign of the
second parameter
math.dist() Returns the Euclidean distance between two points (p and q), where p and q are the
coordinates of that point
math.expm1() Returns Ex - 1
math.fsum() Returns the sum of all items in any iterable (tuples, arrays, lists, etc.)
math.isclose() Checks whether two values are close to each other, or not
math.ldexp() Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i
math.log() Returns the natural logarithm of a number, or the logarithm of number to base
math.perm() Returns the number of ways to choose k items from n items with order and without
repetition
math.remainder() Returns the closest value that can make numerator completely divisible by the
denominator
Math Constants:
Constant Description
The math.sqrt() method for example, returns the square root of a number:
Exercise 3
Write a Python program to calculate the following expressions:
sin 𝜋+cos 𝜋
a. b. ln 𝑒 2 + log10 1000 + log 2 8 + log 5 125
tan𝜋⁄4
Homework
Do the examples of all math functions and constants of the above section 13.2.
-- THE END --