Data Types in Python: Strings, Numbers, and Lists
In Python, data types define the nature of the values that can be stored and manipulated in a program. Understanding key data types like strings, numbers, and lists is fundamental to working with Python effectively.
1. Strings:
Definition: A string is a sequence of characters, and it is used to represent text in Python. Strings are enclosed in single ('
) or double ("
) quotes.
Example:
message = "Hello, Python!"
Operations:
- Concatenation: Combining strings using the
+
operator.
- Length: Finding the length of a string using the
len()
function.
- Indexing and Slicing: Accessing individual characters or portions of a string.
2. Numbers:
Definition: Numbers in Python include integers (whole numbers) and floats (decimal numbers).
Examples:
integer_number = 42 float_number = 3.14
Operations:
- Arithmetic: Performing basic arithmetic operations such as addition, subtraction, multiplication, and division.
- Modulo: Getting the remainder of a division using the
%
operator.
3. Lists:
Definition: A list is an ordered collection of items. It can contain elements of different data types, and the elements can be modified.
Example:
fruits = ["apple", "orange", "banana"]
Operations:
- Accessing Elements: Retrieving elements from a list using indexing.
- Slicing: Extracting a portion of a list.
- Modifying Elements: Changing the value of an element in the list.
- Adding Elements: Appending or inserting new elements into a list.
Comments
Post a Comment