Data types in Python
Let us take a look at what data types do we have in Python
What is a datatype?
A data type is a classification of data that tells the interpreter or compiler how the programmer intends to use the data.
Python is a dynamically-typed language, which means you don't need to declare the data type of a variable explicitly.
Types of datatype in Python
Some common data types in Python include:
int: Integer type, e.g.,
3
,-42
.float: Floating-point type, e.g.,
3.14
,-0.5
.str: String type, e.g.,
'hello'
,"world"
.bool: Boolean type, either
True
orFalse
.list: Ordered, mutable sequence, e.g.,
[1, 2, 3]
.tuple: Ordered, immutable sequence, e.g.,
(1, 2, 3)
.dict: Dictionary type, a collection of key-value pairs, e.g.,
{'key': 'value'}
.set: Unordered collection of unique elements, e.g.,
{1, 2, 3}
.
What are in-built functions?
These are functions or routines that are provided as part of the core programming language and are available for use without requiring explicit importing or external libraries.
In Python, built-in functions are functions that are part of the core Python language and are always available for use without the need for importing any external modules.
Some built in functions:
Here are some examples of built-in functions:
len()
: Returns the length (the number of items) of an object. Works with sequences like strings, lists, and tuples.
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print(length) # Output: 5
print()
: Outputs the specified value(s) to the console.
print("Hello, World!")
max()
andmin()
: Return the maximum and minimum values, respectively, of an iterable.
numbers = [1, 2, 3, 4, 5]
maximum = max(numbers)
minimum = min(numbers)
print(maximum, minimum) # Output: 5 1
A task
Let us suppose you are given a task that you have been given the list of full names of employees of your org and you are asked to print their first names only. Suppose the list of names is as follows:
employee_names = [
"Naved Ahmad",
"John Doe",
"Jane Smith",
# Add more names as needed
]
The code to retrieve the first names only is as follows:
employee_names = [
"Naved Ahmad",
"John Doe",
"Jane Smith",
# Add more names as needed
]
for full_name in employee_names:
first_name = full_name.split()[0]
print(first_name)
Output will be,
Naved
John
Jane
Here, split()
is used without specifying any argument, and it defaults to splitting the string at whitespace. This way, full_name.split()
will return a list of words in the full name, and full_name.split()[0]
will give you the first name.
If your data has a different structure, you might need to adjust the code accordingly. For instance, if the names are separated by commas or some other character, you can pass that character as an argument like split(",")
.
String manipulation in Python
String manipulation refers to the process of modifying or manipulating strings, which are sequences of characters.
In programming, string manipulation is a common task, and it involves performing operations on strings to achieve specific outcomes. This can include tasks such as searching, replacing, concatenating, splitting, and formatting strings.
Some String manipulation functions in Python
Here are some string manipulation functions in Python:
- String Length
len()
: Thelen()
function returns the length (number of characters) of a string.
text = "Python is fun"
length = len(text)
print("Length of the string:", length)
# Output: Length of the string: 14
- String Slicing: Slicing allows you to extract a portion of a string. It is done using square brackets and indices.
sentence = "Programming is enjoyable"
fragment = sentence[0:11] # Extracts characters from index 0 to 10 (excluding 11)
print(fragment)
# Output: "Programming"
- Splitting at Whitespace (Default):
text = "Python is awesome"
words = text.split()
print(words)
# Output: ['Python', 'is', 'awesome']
What's next?
In the next article in this blog series, we will be looking at keywords and variables in python.
Stay tuned.