Python data types:
Basic Syntax in Python:
1. Indentation:
2. Comments:
3. Variables:
4. Print Statement:
5. Input:
6. Conditional Statements:
7. Loops:
8. Functions:
Basic Data Types in Python:
1. Integers:
2. Floats:
3. Complex Numbers:
4. Strings:
5. Booleans:
6. Lists:
7. Tuples:
8. Dictionaries:
9. Set:
FAQ:
You can use the type()
function to check the type of a variable:
x = 10
print(type(x)) # Output: <class ‘int’>
You can use the int()
function for string-to-integer conversion:
string_number = “123”
integer_number = int(string_number)
The str()
function is used to convert values of other data types to strings. For example:
number = 42
string_number = str(number)
You can use triple quotes ('''
or """
) to create multiline strings:
multiline_string = ”’
This is a multiline
string in Python.
”’
The **
operator is used for exponentiation in Python:
result = 2 ** 3 # Result: 8
A list is a versatile data type in Python that can hold a collection of items. It is defined using square brackets:
my_list = [1, 2, 3, “apple”, True]
Elements in a list are accessed using index values. Indexing starts from 0:
my_list = [10, 20, 30, 40]
print(my_list[1]) # Output: 20
A tuple is an ordered and immutable collection of items in Python. It is defined using parentheses:
my_tuple = (1, 2, 3, “banana“)
The isinstance()
function is used to check if a variable is of a specific data type:
x = 42
print(isinstance(x, int)) # Output: True
A set is an unordered and unique collection of items. It is defined using curly braces:
my1_set = {1, 2, 3, “apple”}
None
is a special constant in Python representing the absence of a value or a null value. It is often used to signify that a variable has no assigned value.
x = None
You can use the in
keyword to check if a key exists in a dictionary:
my_dict = {“name”: “John”, “age”: 25, “city”: “New York”}
if “age” in my_dict:
print(“Age is present in the dictionary.”)
You can use the tuple()
constructor to convert a list to a tuple:
my_list = [1, 2, 3, 4]
my_tuple = tuple(my_list)
The len()
function returns the length (the number of items) of an object, such as a string, list, tuple, or dictionary.
my_list = [1, 2, 3, 4, 5]
length = len(my_list) # Output: 5