eng. ali abu harb - islamic university of gaza

10
Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Introduction to Computers Lab (LNGG 1003) _____________________________________________________________________________________ Eng. Ali Abu Harb

Upload: others

Post on 28-Mar-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Introduction to Computers Lab (LNGG 1003) _____________________________________________________________________________________

Eng. Ali Abu Harb

Python Lists:

Lists are Python's compound data types. A list contains items separated by commas and

enclosed within square brackets ([]).

All the items belonging to a list can be of different data type.

The values stored in a list can be accessed using the slice operator ([ ] and [:]) with

indexes starting at 0 in the beginning of the list and working their way to end -1.

Example 1:

List Methods:

name Description

count ( value ) Returns the number of times a given value appears in the list

Index( value) Returns the lowest index of a given element (value ) within the list

extend(seq ) This method does not return any value but add the content sequence to

existing list.

insert(index,object) Inserts a new element (object) before the element at a given index.

Increases the length of the list by one.

append ( object ) Adds a new element (object) to the end of the list.

remove(value) Removes the first occurrence (lowest index) of a given element (value)

from the list. Produces an error if the element is not found.

reverse() reverses the elements in the list. The list is modified

sort Sorts the elements of the list in ascending order. The list is modified

Example 2:

Python Tuples:

A tuple is another sequence data type that is similar to the list. A tuple consists of a

number of values separated by commas, tuples are enclosed within parentheses ().

Tuples its read only, that’s means are immutable.

The main differences between lists and tuples are: Lists are enclosed in brackets ([ ]) and their

elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot

be updated.

Tuple Methods:

name Description

count ( value ) Returns the number of times a given value appears in the tuple

Index( value) Returns the lowest index of a given element (value ) within the tuple

Examples:

Example 3:

Range of Negative Indexes

Specify negative indexes if you want to start the search from the end of the tuple.

Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Example 4:

Python Dictionary:

Python's dictionaries are kind of hash table type consist of key-value pairs.

A dictionary key can be almost any Python type, but are usually numbers or strings.

A dictionary Values can be any arbitrary Python object.

Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed

using square braces ([]).

Dictionaries have the elements are "out of order", they are simply unordered. Examples:

Example 3:

Example 4:

Mathematical functions:

1. Build-in Functions

The Python interpreter has a number of functions built into it that are always available:

abs(x) The absolute value of x.

max( seq ) return its largest item in sequence (String ,List, Tuple )

min( seq) return its smallest item n sequence (String ,List, Tuple )

sum (seq ) return the sum of a sequence (List, Tuple) of numbers

len(seq) Return number of elements in sequence (String ,List, Tuple )

Output:

print "abs(-5) = ",abs(-5)

print "list=[88,5,9], largest number in list is = ",max([88,5,9])

print "list=[88,5,9], smallest number in list is = ",min([88,5,9])

print "sum of a sequence of numbers [8,6,2,5] = ",sum([8,6,2,5])

2. Math module functions: The following functions are provided by this module, before use it we must invoke

module using import keyword:

ceil(x) Return the ceiling of x as a float, the smallest integer value greater

than or equal to x.

floor(x) Return the floor of x as a float, the largest integer value less than or

equal to x.

fabs(x) Return the absolute value of x as a float.

sqrt(x) The square root of x for x > 0

log(x) The natural logarithm of x, for x> 0

log10(x) The base-10 logarithm of x for x> 0

Trigonometric Functions

sin(x), cos(x), tan(x)…

Return value of function in radians

radians(x) Converts angle x from degrees to radians.

degrees(x) Converts angle x from radians to degrees.

exp(x) The exponential of x: ex

Constants: pi The mathematical constant π = 3.141592...

e The mathematical constant e = 2.718281...

How to use it: See the next page