Data Types:
Data types are an essential concept in programming. They represent the different types of data that a program needs to manipulate and process.
Python, like many programming languages, supports several built-in data types. Here are some of the common data types in Python:
Data Types in Python
Python supports a variety of data types:
Numeric types:
int
,float
,complex
Sequence types:
list
,tuple
,range
Mapping type:
dict
Set type:
set
,frozenset
Boolean type:
bool
Binary types:
bytes
,bytearray
,memoryview
None type:
NoneType
Python data types determine how data is stored and operated upon.
Numeric Types
int
: Stores integers.
COPY
a = 10
type(a)
# <class 'int'>
float
: Stores floating point numbers.
COPY
b = 10.5
type(b)
# <class 'float'>
complex
: Stores complex numbers with real and imaginary parts.
COPY
c = 1 + 2j
type(c)
# <class 'complex'>
Sequence Types
list
: Stores ordered collections of items.
COPY
fruits = ["Apple", "Banana", "Cherry"]
type(fruits)
# <class 'list'>
tuple
: Stores immutable ordered collections.
COPY
coordinates = (3, 5)
type(coordinates)
# <class 'tuple'>
range
: Generates a sequence of numbers.
Mapping Type
dict
: Stores data as key-value pairs.
COPY
capital_city = {'Nepal': 'Kathmandu', 'Italy': 'Rome'}
type(capital_city)
# <class 'dict'>
Other Types
set
: Stores unordered collections of unique items.bool
: StoresTrue
orFalse
values.str
: Stores string values.NoneType
: Represents the absence of a value.
Data types allow Python to:
Allocate memory efficiently
Perform type checking
Specify function parameters Hope this helps! Let me know if you have any other questions.
String Data Type in Python
In Python, a string is a sequence of characters. Strings are defined either using single quotes ('...') or double quotes ("...") with the difference being that double quotes allow interpolation of variables using the $ sign.
COPY
string1 = 'Hello' string2 = "Hello"
Strings are immutable, which means you cannot change an individual character in a string, you have to create a new string.
Some key facts about strings in Python:
Strings can be concatenated using the + operator or the format() method
Strings can be sliced using [] to get substrings
The length of a string can be found using the len() function
Strings can be checked for substrings using the in and not in operators
Strings have many useful methods like upper(), lower(), split(), replace(), etc.
For example:
COPY
string = "Hello World!"
# Concatenate
string2 = string + " Welcome!"
# Slice
string3 = string[2:5]
# Length
length = len(string)
# Check
if "Hello" in string:
print("Yes")
# Upper case
string_upper = string.upper()
# Replace
string_new = string.replace("World", "Universe")
In the web search result, we also saw:
Strings can be assigned to variables
Multiline strings can be defined using triple quotes
Strings can be indexed and sliced like arrays
String Manipulation and Formatting:
Concatenation: You can combine strings using the
+
operator.Substrings: Use slicing to extract portions of a string, e.g.,
my_string[2:5]
will extract characters from the 2nd to the 4th position.String interpolation: Python supports various ways to format strings, including f-strings (f"...{variable}..."), %-formatting ("%s %d" % ("string", 42)), and
str.format()
.Escape sequences: Special characters like newline (\n), tab (\t), and others are represented using escape sequences.
String methods: Python provides many built-in methods for string manipulation, such as
split()
,join()
, andstartswith()
.
Regex
Regular expressions (or regex) are a powerful tool for pattern matching in strings. The re
module in Python's standard library provides robust support for regex.
Some of the main things you can do with regex in Python are:
Match patterns in strings
Search for patterns in strings
Split strings based on patterns
Replace patterns in strings
The basics of using regex in Python are:
import re
This imports the re
module. Then you can compile a regex pattern:
pattern = re.compile(r'some regex pattern')
The r
before the regex string means "raw string" - escaping backslashes is not needed.
You can then use this pattern to:
- Match:
result = pattern.match(some_string)
- Search:
result = pattern.search(some_string)
- Split:
result = pattern.split(some_string)
- Substitute:
result = pattern.sub('replacement', some_string)
The result in each case is either a Match
object or a list/string.
To define regex patterns, you can use:
.
- Any single character[abc]
- Any ofa
,b
orc
a*
- Zero or morea
sa+
- One or morea
sa?
- Zero or onea
\d
- Any digit
For example, to match any number, you can use:
\d+
That's a wrap......................