Python Datatypes.docx
Python Datatypes.docx
The data stored in memory can be of many types. For example, a person's name is stored as
alphabets, age is stored as a numeric value and his or her address is stored as alphanumeric
characters. Python has the following standard data types.
.1 Numbers
The numeric data type in Python represents the data that has a numeric value. A numeric value
can be an integer, a floating number, or even a complex number. These values are defined as
Python int , Python float , and Python complex classes in Python .
● Integers – This value is represented by int class. It contains positive or negative whole
numbers (without fractions or decimals). In Python, there is no limit to how long an integer
value can be.
● Float – This value is represented by the float class. It is a real number with a floating-point
representation. It is specified by a decimal point. Optionally, the character e or E followed by a
positive or negative integer may be appended to specify scientific notation.
● Complex Numbers – A complex number is represented by a complex class. It is specified as
(real part) + (imaginary part)j . For example – 2+3j
Example:
a=5
print(type(a))
b = 5.0
print(type(b))
c = 2 + 4j
print(type(c))
Output
<class 'int'>
<class 'float'>
<class 'complex'>
Sequence Data Types in Python
The sequence Data Type in Python is the ordered collection of similar or different Python
data types. Sequences allow storing of multiple values in an organized and efficient fashion.
There are several sequence data types of Python:
● Python String
● Python List
● Python Tuple
● String Data Type
String
Python Strings are arrays of bytes representing Unicode characters. In Python, there is no
character data type Python, a character is a string of length one. It is represented by str class.
Strings in Python can be created using single quotes, double quotes, or even triple quotes.
We can access individual characters of a String using index.
s = 'Welcome to the Geeks World'
print(s)
Example:
# check data type
print(type(s))
output
Welcome to the Geeks World
<class 'str'>
e
l
d
1.String Creation
String operations
Indexing
Example:
text = "Python"print(text[0]) # P (first character)print(text[1]) # y (second
character)print(text[-1]) # n (last character)print(text[-2]) # o (second last character)
Output
P
Y
N
O
Slicing
Examples:
窗体底端
#Demo of len(string)
s='Learning Python is fun!'
print("Length of", s, "is", len(s))
Output
Code
#Demo of lower()
s='Learning Python is fun!'
print(s.lower())
Output:
learning python is fun!
■ upper() - Returns a copy of the string in which all lowercase alphabets in a string are
converted to uppercase alphabets.
Example Program
#Demo of upper()
s="learning Python is fun!"
print(s.upper())
Output:
LEARNING PYTHON IS FUN!
■ swapcase() - Returns a copy of the string in which the case of all the alphabets are
swapped, i.e., all the lowercase alphabets are converted to uppercase and vice versa.
Example Program
#Demo of swapcase()
s="LEARNing PYTHON is fun!"
print(s.swapcase())
Output:
learnING python IS FUN!
■ capitalize() - Returns a copy of the string with only its first character capitalized.
Example Program
#Demo of capitalize()
s="learning Python is fun!"
print(s.capitalize())
Output:
Learning Python is fun!
■ title() - Returns a copy of the string in which the first character of all the words are
capitalized.
Example Program
#Demo of title()
s="learning Python is fun!"
print(s.title())
Output:
Learning Python Is Fun!
■ lstrip() - Returns a copy of the string in which all the characters have been stripped
(removed) from the beginning. The default character is whitespaces.
Example Program:
#Demo of lstrip()
s=" learning Python is fun!"
print(s.lstrip())
s="****learning Python is fun!"
print(s.lstrip("*"))
Output:
#Demo of rstrip()
s=" learning Python is fun! "
print(s.rstrip())
s="learning Python is fun!**"
print(s.rstrip("*"))
Output:
Code
#Demo of strip()
s=" learning Python is fun! "
print(s.strip())
s="***learning Python is fun!***"
print(s.strip("*"))
Output:
#Demo of max(str)
s='learning Python is fun'
print('Maximum character is:', max(s))
Output
Maximum character is: y
■ min(str) - Returns the minimum alphabetical character from string str.
Example Program
#Demo of min(str)
s='learning-Python-is-fun'
print('Minimum character is:', min(s))
Output
Minimum character is: -
■ replace(old, new [,max]) - Returns a copy of the string with all the occurrences of
substring old is replaced by new. The max is optional and if it is specified, the first
occurrences specified in max are replaced.
Example Program
#Demo of replace(old, new[,max])
s="This is very new. This is good"
print(s.replace('is', 'was'))
print(s.replace('is', 'was', 2))
Output
Thwas was very new. Thwas was good
Thwas was very new. This is good
■ center(width, fillchar) - Returns a string centered in a length specified in the width
variable. Padding is done using the character specified in the fillchar. Default padding is
space.
Example Program
#Demo of center(width, fillchar)
s="This is Python Programming"
print(s.center(30, '*'))
print(s.center(30))
Output
*This is Python Programming*
This is Python Programming
14. ljust(width[,fillchar]) - Returns a string left-justified in a length specified in the width
variable. Padding is done using the character specified in the fillchar. Default padding is
space.
Example Program
■ String Concatenation
Example:
python
CopyEdit
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2 # Adding a space between stringsprint(result) # Output:
Hello World
String Repetition
Example:
python
CopyEdit
str1 = "Hello"
result = str1 * 3 # Repeats the string 3 timesprint(result) # Output: HelloHelloHello