Python is the most popular programming language. It has various types of data types to handle the different kinds of data. If you are a beginner then you must have a deep understanding of basic data types in Python. In this tutorial, you will know how to use Use Basic Data Types in Python.
Basic Data Types in Python
Numbers
Python supports various types of numbers, including integers and floating-point numbers. Integers are whole numbers without any decimal point, while floating-point numbers are numbers with a fractional component.
To declare an integer variable in Python, you can simply assign a value to a variable name. For example:
x = 100
To declare a floating-point variable, you can use the same syntax but include a decimal point in the value:
y = 13.14
Python also provides a built-in function called type()
that allows you to check the type of a variable. For example:
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'float'>
Strings
Strings are sequences of characters enclosed in single quotes ('
) or double quotes ("
). They are used to represent textual data in Python.
To declare a string variable, you can assign a sequence of characters to a variable name using quotes. For example:
name = "Code The Best"
You can perform various operations on strings, such as concatenation and slicing. For example, to concatenate two strings, you can use the +
operator:
greeting = "Hello"
name = "Code The Best"
message = greeting + ", " + name + "!"
print(message) # Output: Hello Code The Best!
To access individual characters in a string, you can use indexing. Python uses zero-based indexing, so the first character is at index 0. For example:
name = "Python"
print(name[0]) # Output: P
print(name[2]) # Output: t
Lists
Lists are ordered collections of items enclosed in square brackets ([]
). They can store items of different types and allow for dynamic resizing.
To declare a list variable, you can assign a sequence of items to a variable name using square brackets. For example:
fruits = ["apple", "banana", "orange"]
You can access individual items in a list using indexing, similar to strings. For example:
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: orange
Lists provide several methods to manipulate and modify their contents, such as append()
, insert()
, and remove()
. For example:
fruits.append("grape") # Add an item to the end of the list
print(fruits) # Output: ['apple', 'banana', 'orange', 'grape']
fruits.insert(1, "kiwi") # Insert an item at a specific position
print(fruits) # Output: ['apple', 'kiwi', 'banana', 'orange', 'grape']
fruits.remove("banana") # Remove a specific item from the list
print(fruits) # Output: ['apple', 'kiwi', 'orange', 'grape']
Tuples
Tuples are ordered collections of items enclosed in parentheses (()
). They are similar to lists but are immutable, meaning their contents cannot be modified after creation.
To declare a tuple variable, you can assign a sequence of items to a variable name using parentheses. For example:
colors = ("red", "green", "blue")
You can access individual items in a tuple using indexing, similar to lists and strings. For example:
print(colors[0]) # Output: red
print(colors[2]) # Output: blue
Tuples are commonly used when you want to group related values together that should not be changed.
Booleans
Booleans represent the truth values True
and False
in Python. They are used for logical operations and comparisons.
To declare a boolean variable, you can assign either True
or False
to a variable name. For example:
is_study_completed = True
is_homework_submitted = False
Booleans are essential for conditional statements, where different parts of the code are executed based on the truth value of certain conditions.
Conclusion
In this tutorial, you learned the basic data types in Python and how you can use them effectively. Knowing what data types do is very important for writing efficient Python programs. By mastering the Python basic concepts you will be able to handle it and build powerful applications.
Leave a Reply