Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Lab01 Python1

Uploaded by

letrungbao2502bt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab01 Python1

Uploaded by

letrungbao2502bt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Ton Duc Thang University

Faculty of Information Technology

Applied Calculus for IT - 501031


Python tutorial 01

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:

Trinh Hung Cuong - Applied Calculus for IT - 501031 1/15


Ton Duc Thang University
Faculty of Information Technology
Simple as that. Save your file. Open your command line, navigate to the directory where you saved
your file (for ex., in the drive D:\), and run:

The output is:

4. Python on Google Colab


You can access the website https://colab.research.google.com/ to write and execute Python in your
browser.

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:

- Python will give you an error if you skip the indentation:

The content of error is: IndentationError: expected an indented block

- The number of spaces is up to you as a programmer, but it has to be at least one.

- You have to use the same number of spaces in the same block of code, otherwise Python will
give you an error:

Trinh Hung Cuong - Applied Calculus for IT - 501031 2/15


Ton Duc Thang University
Faculty of Information Technology

The content of error is: IndentationError: unexpected indent

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:

- Multi Line Comments

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.

Trinh Hung Cuong - Applied Calculus for IT - 501031 3/15


Ton Duc Thang University
Faculty of Information Technology
- Casting
If you want to specify the data type of a variable, this can be done with casting.

- 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)

Legal variable names:

Illegal variable names:

Multi Words Variable Names:


Variable names with more than one word can be difficult to read. There are several
techniques you can use to make them more readable:
o Camel Case
Each word, except the first, starts with a capital letter:
Trinh Hung Cuong - Applied Calculus for IT - 501031 4/15
Ton Duc Thang University
Faculty of Information Technology

o Pascal Case
Each word starts with a capital letter:

o Snake Case
Each word is separated by an underscore character:

8. Python Data Types


Python has the following data types built-in by default, in these categories:

You can get the data type of any object by using the type() function:

Trinh Hung Cuong - Applied Calculus for IT - 501031 5/15


Ton Duc Thang University
Faculty of Information Technology
9. Python Numbers
There are three numeric types in Python: int, float, and complex.

Float can also be scientific numbers with an "e" to indicate the power of 10.

Complex numbers are written with a "j" as the imaginary part:

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.

Trinh Hung Cuong - Applied Calculus for IT - 501031 6/15


Ton Duc Thang University
Faculty of Information Technology
In programming you often need to know if an expression is True or False. When you compare two
values, the expression is evaluated and Python returns the Boolean answer:

11.Python Operators
- Python Arithmetic Operators

Operator Name Example

+ 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

Operator Example Same As

= 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

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

- Python Comparison Operators

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

Trinh Hung Cuong - Applied Calculus for IT - 501031 8/15


Ton Duc Thang University
Faculty of Information Technology

>= Greater than or equal x >= y


to

<= Less than or equal to x <= y

x=5
y=3

print(x == y) # returns False because 5 is not equal to 3


print(x >= y) # returns True because five is greater, or equal, to 3

- Python Logical Operators

Operator Description Example

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is true x < 5 or x < 4

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

Trinh Hung Cuong - Applied Calculus for IT - 501031 9/15


Ton Duc Thang University
Faculty of Information Technology
12.Python Functions
A function is a block of code which only runs when it is called. You can pass data, known as
parameters, into a function. A function can return data as a result.
To call a function, use the function name followed by parenthesis, and then parameters can be
passed inside the parentheses.

print(“sum of a and b:”)

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.

13.1 Built-in Math Functions


- The min() and max() functions can be used to find the lowest or highest value in an iterable:

- The abs() function returns the absolute (positive) value of the specified number:

13.2 The Math Module


Python has also a built-in module called math, which extends the list of mathematical functions.
To use it, you must import the math module, and then you can start using methods and constants
of the module.

Trinh Hung Cuong - Applied Calculus for IT - 501031 10/


15
Ton Duc Thang University
Faculty of Information Technology

Math Methods:

Method Description

math.acos() Returns the arc cosine of a number

math.acosh() Returns the inverse hyperbolic cosine of a number

math.asin() Returns the arc sine of a number

math.asinh() Returns the inverse hyperbolic sine of a number

math.atan() Returns the arc tangent of a number in radians

math.atan2() Returns the arc tangent of y/x in radians

math.atanh() Returns the inverse hyperbolic tangent of a number

math.ceil() Rounds a number up to the nearest integer

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.cos() Returns the cosine of a number

Trinh Hung Cuong - Applied Calculus for IT - 501031 11/


15
Ton Duc Thang University
Faculty of Information Technology

math.cosh() Returns the hyperbolic cosine of a number

math.degrees() Converts an angle from radians to degrees

math.dist() Returns the Euclidean distance between two points (p and q), where p and q are the
coordinates of that point

math.erf() Returns the error function of a number

math.erfc() Returns the complementary error function of a number

math.exp() Returns E raised to the power of x

math.expm1() Returns Ex - 1

math.fabs() Returns the absolute value of a number

math.factorial() Returns the factorial of a number

math.floor() Rounds a number down to the nearest integer

math.fmod() Returns the remainder of x/y

math.frexp() Returns the mantissa and the exponent, of a specified number

math.fsum() Returns the sum of all items in any iterable (tuples, arrays, lists, etc.)

math.gamma() Returns the gamma function at x

math.gcd() Returns the greatest common divisor of two integers

math.hypot() Returns the Euclidean norm

math.isclose() Checks whether two values are close to each other, or not

math.isfinite() Checks whether a number is finite or not

math.isinf() Checks whether a number is infinite or not

math.isnan() Checks whether a value is NaN (not a number) or not

Trinh Hung Cuong - Applied Calculus for IT - 501031 12/


15
Ton Duc Thang University
Faculty of Information Technology

math.isqrt() Rounds a square root number downwards to the nearest integer

math.ldexp() Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i

math.lgamma() Returns the log gamma value of x

math.log() Returns the natural logarithm of a number, or the logarithm of number to base

math.log10() Returns the base-10 logarithm of x

math.log1p() Returns the natural logarithm of 1+x

math.log2() Returns the base-2 logarithm of x

math.perm() Returns the number of ways to choose k items from n items with order and without
repetition

math.pow() Returns the value of x to the power of y

math.prod() Returns the product of all the elements in an iterable

math.radians() Converts a degree value into radians

math.remainder() Returns the closest value that can make numerator completely divisible by the
denominator

math.sin() Returns the sine of a number

math.sinh() Returns the hyperbolic sine of a number

math.sqrt() Returns the square root of a number

math.tan() Returns the tangent of a number

math.tanh() Returns the hyperbolic tangent of a number

math.trunc() Returns the truncated integer parts of a number

Math Constants:

Trinh Hung Cuong - Applied Calculus for IT - 501031 13/


15
Ton Duc Thang University
Faculty of Information Technology

Constant Description

math.e Returns Euler's number (2.7182...)

math.inf Returns a floating-point positive infinity

math.nan Returns a floating-point NaN (Not a Number)


value

math.pi Returns PI (3.1415...)

math.tau Returns tau (6.2831...)

The math.sqrt() method for example, returns the square root of a number:

The math.pi constant, returns the value of PI (3.14...):

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.

Trinh Hung Cuong - Applied Calculus for IT - 501031 14/


15
Ton Duc Thang University
Faculty of Information Technology
14.References
- Python Tutorial on the W3schools website: https://www.w3schools.com/python/default.asp
- Python Tutorial on the Tutorials Point website:
https://www.tutorialspoint.com/python/index.htm

-- THE END --

Trinh Hung Cuong - Applied Calculus for IT - 501031 15/


15

You might also like