0% found this document useful (0 votes)
20 views

Lap2 Eng

The document discusses Python data types including strings, numbers, lists, tuples and dictionaries. It provides examples of built-in functions and methods to manipulate and work with each data type. It also covers input/output functions in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lap2 Eng

The document discusses Python data types including strings, numbers, lists, tuples and dictionaries. It provides examples of built-in functions and methods to manipulate and work with each data type. It also covers input/output functions in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PYTHON

Lap 2

Third level

Eng\Seham AL-Bhloli
❖ Data type
1. (string)
Texts in Python can be defined by (variable name = 'text', or variable
name = 'text'). In the sense that we can put the text in one single quotes or
double quotes or three quotation marks, which are called multiline string
, as we will show in the examples:-
Ex1:
s="welcome in this lap"
print(s)
print(s[0])
print(s[1])
print(s[7])
print(s[8:16])
print(s[-5])
print(s[:3])
print(s*2)
print(s + "number 2")
Now let's come to explain the length of the text (String Length),
where the length of the text can be calculated through the internal
function of Python, which is called (len()), which works to calculate
the number of characters in the text
Ex2:
X="python"
Print(len(x))
Ex3:
s=" AliAhmed"
for a in range(len(s)):
print s[0:a+1]
Python also contains a set of functions that can be used in texts with
ease, and they are many and varied, of which we will mention the most
common:

Lower()
This function converts characters in the text string to lowercase.
Ex:
s ="HELLO, PYTHON ! "
Print(s.lower())

Upper()
This function converts characters in the text string to uppercase.
Ex:
s ="hello, python ! "
Print(s.upper())

Replace()
This function changes the sentence or letters chosen by the user to
other sentences and letters.
Ex:
s ="hello, python ! "
Print(s.replace("python ", "world"))
Count()
This function returns the number of occurrences of a particular
character or sentence in texts
Ex:
s ="hello, python ! "
Print(s.count("h"))
Isdigit()
Check whether text is numbers
Ex:
x= "hello, python ! "
y="123456789"
Print(s.isdigit())
Print(s. isdigit ())
rStrip()
It removes any excess spaces.
Ex:
x ="mohmmed "
y=x.rstrip()
Print("my name is", y,"!")
Swapcase()
This function makes uppercase lowercase and uppercase convert them
to lowercase (i.e. switch case to letter)
Ex:
s = ("Hello Python")
print(s.swapcase())
2. (numbers)
Each number you enter into Python will be interpreted as a number
that you are not required to explicitly declare the type of data you
enter because Python counts any number written without decimal
separators as an integer as an integer as 139 ) and any number
written with decimal separators as a decimal number (float as the
number 139.0 )
That is, in Python, we can write the name of the variable and assign the
value to it without specifying the type of variable
Examples include:
x=139
y=-65
f=139.0
z=56.5
print("x = ",x)
print("y = ",y)
print("f = ",f)
print("z = ",z)

(Number Random )

By calling his Random library and through it we generate a random number


and in the event of repeated execution you will give a number within the
specified range

Import random
print (random.randrange(1,10))
3. (List)
A list is a changeable sequence and text strings are defined using
quotation marks and lists are defined using parentheses [ ]
Ex1:
List=[1,'teriq',33.3,"python"]
Print(list)
Print(list[2])
Print(list[1:3])
list[2]="ali"
print(list)
Ex2:
index=1
name=['ali','ahmed','moahmed','saleh']
age=[10,20,30,40]
for a in age:
name.insert(index,a)
index=index+2
print(name)

To calculate the number of items in a list, we use the len() function


Ex:
mylist=["python","c++", "c#", "java"]
print(len(mylist))
Add Items Function append()
Ex:
Mylist=[]
Print(Mylist)
Mylist.append("python3")
Print(Mylist)
Mylist.append("c#")
Print(Mylist)
We note that the append() function works to add elements at the end of
the list only, but we can also add specific elements with a specific
location depending on the index, i.e. specified index through the insert()
function

Ex:
mylist=["python","c++", "c#", "java"]
print(mylist)
mylist.insert(2,"sql")
print(mylist)
The remove() function deletes an element
Ex:
mylist=["python","c++", "c#", "java"]
print(mylist)
mylist. remove ("python")
print(mylist)

pop() functionDeletes an element from the top (without specifying)


mylist=["python","c++", "c" ,"java"]
print(mylist)
mylist. pop ()
print(mylist)
clear() function deletes all items from the list and makes the list empty
mylist=["python","c++", "c" ,"java"
print(mylist)
mylist. clear ()
print(mylist)
Two lists can be combined through (+)
Ex:
List1=["python" , "c++"]
List2=["java" , "c#"]
List3= List1+ List2
Print(List3)
The extend() function merges two lists
Ex:
List1=["python" , "c++"]
List2=["java" , "c#"]
List1.extend(List2)
Print(List1)
Reverse() function inverting list function
Ex:
mylist=["python","c++", "c#", "java"]
print(mylist)
mylist. reverse ()
print(mylist)
sort() function order list
Ex:
mylist=["python","c++", "c#", "java"]
print(mylist)
mylist. sort ()
print(mylist)
4. (Tuple)
The tuple is used to group data, which is a fixed sequence of elements and
is immutable Rows are very similar to lists, but use parentheses() instead
of square brackets [],
Because they are immutable, their value cannot be changed or modified.
Ex:
color = ('blue,' red', 'yellow' ,'black')
print(color)
Ex:
color = ('blue,' red', 'yellow' ,'black')
print(color[1])
5. (Dictionaries)
A dictionary is an unstructured, changeable and indexable collection. In
Python, dictionaries are written in zigzag brackets { } and have what is
known as (keys and values) where the key and value are separated by two
perpendicular points (key:value). These keys and values are very
important in the dictionary.
Ex:
Nums = {1:"one",2:"two",3:"three"}
Print(Nums)
Print(Nums[1])
Print(Nums[2])
Print(Nums[3])
Ex:
a="abcdabcdabcdabcdabcdabcdddccbaabcdbacbd"
dictionary={}
for letter in a:
if letter in dictionary: dictionary[letter]+=1
else: dictionary[letter]=1
print(dictionary)
❖ Input and output in Python
In Python there are two functions, one for output for printing and
called (print) and the second for input called (input).
Ex:
S= input("enter number: ")
Print(S)
In the above example we can see that the (input) used for input by
the user is very flexible induced can enter a sentence character
number... Print works to output data to the screen.
On the other hand, we can define the entry with an integer
number, decimal number or sentence by simply writing int , str
andfloat before the input statement and to represent this as follows:
Ex:
S=int( input("enter number: "))
Print(S)
Ex:
S=float( input("enter number: "))
Print(S)
Ex:
S=str( input("enter number: "))
Print(S)
Ex:
n1=int(input(' enter n 1'))
n2=int(input(' enter n 2'))
print(n1,"+ ",n2,"= ",n1+n2)
❖Questions
Q1:Write a program that returns all uppercase
letters into a list and returns all lowercase letters
into another list of the following phrase: WelCome
IN Python witH Lap twO
Type a program that contains the following list:Q2:
List=[5,9,100,5,80,30,4,5,80,90]
Then delete the duplicate numbers within the list.

You might also like