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