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

Python Datatypes.docx

The document provides an overview of Python data types, focusing on numeric types (integers, floats, and complex numbers) and sequence types (strings, lists, and tuples). It details string operations such as indexing, slicing, and various built-in functions for string manipulation. Additionally, it covers string concatenation and repetition, illustrating these concepts with examples.

Uploaded by

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

Python Datatypes.docx

The document provides an overview of Python data types, focusing on numeric types (integers, floats, and complex numbers) and sequence types (strings, lists, and tuples). It details string operations such as indexing, slicing, and various built-in functions for string manipulation. Additionally, it covers string concatenation and repetition, illustrating these concepts with examples.

Uploaded by

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

Python datatypes

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

# access string with index


print(s[1])
print(s[2])
print(s[-1])

output
Welcome to the Geeks World
<class 'str'>
e
l
d

1.String Creation
String operations

string1 = "Hello, World!"


string2 = 'Python is fun'

Indexing

●​ Each character in a Python string is assigned a unique position or index.


●​ Positive indices start from 0 for the first character, 1 for the second, and so on.
●​ Negative indices start from -1 for the last character, -2 for the second last, and so on.

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

●​ Slicing allows extracting substrings from a string.


●​ The syntax for slicing is:​
string[start:end:step]

●​ start: The starting index (inclusive).


●​ end: The stopping index (exclusive).
●​ step: Optional; defines the jump between characters.

Examples:

text = "PythonProgramming"# Simple slices


print(text[0:6]) # Python (from index 0 to 5)
print(text[6:]) # Programming (from index 6 to the end)
print(text[:6]) # Python (from start to index 5)
print(text[0:14:2]) # (every second character)
# Negative slicing
print(text[-11:-3]) # (substring using negative indices)
print(text[::-1]) # prognimmargorPnohtyP (reverse the string)

窗体底端

Python includes a large number of built-in functions to manipulate strings.


■​ len(string)- Returns the length of the string
Example Program

#Demo of len(string)
s='Learning Python is fun!'
print("Length of", s, "is", len(s))
Output
Code

Length of Learning Python is fun! is 23


■​ lower()-Returns a copy of the string in which all uppercase alphabets in a string are
converted to lowercase alphabets.
Example Program

#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:

learning Python is fun!


learning Python is fun!
■​ rstrip() - Returns a copy of the string in which all characters have been stripped
(removed) from the end. The default character is whitespace.
Example Program:

#Demo of rstrip()
s=" learning Python is fun! "
print(s.rstrip())
s="learning Python is fun!**"
print(s.rstrip("*"))
Output:
Code

learning Python is fun!


learning Python is fun!
■​ strip() - Returns a copy of the string in which all characters have been stripped
(removed) from the beginning and end. It performs both lstrip() and rstrip(). The default
character is whitespace.
Example Program:

#Demo of strip()
s=" learning Python is fun! "
print(s.strip())
s="***learning Python is fun!***"
print(s.strip("*"))
Output:

learning Python is fun!


learning Python is fun!
■​ max(str) - Returns the maximum alphabetical character from string str.
Example Program:
Python

#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

●​ Joining two or more strings together using the + operator.

Example:
python
CopyEdit
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2 # Adding a space between stringsprint(result) # Output:
Hello World

●​ Strings must be combined explicitly using +.


●​ The result is a new string, as strings in Python are immutable.

Concatenating Variables and Literals:


greeting = "Hi"print(greeting + ", Alice!") # Output: Hi, Alice!

String Repetition

●​ Repeating a string multiple times using the * operator.

Example:
python
CopyEdit
str1 = "Hello"
result = str1 * 3 # Repeats the string 3 timesprint(result) # Output: HelloHelloHello

You might also like