Python data types:

Basic Syntax in Python:

The design of Python’s syntax prioritizes clarity and readability. The following outlines key elements of Python syntax:
 
1. Indentation:
Python uses indentation (whitespace) to define code blocks, eliminating the need for braces or keywords. Appropriate indentation plays a vital role in enhancing the readability of code.
 
if x > 0: 
print(“Positive number”)
 
2. Comments:
Comments begin with the # symbol and are used to explain code. They are ignored by the interpreter.
 
# This is a comment
 
3. Variables: 
The creation of variables involves assigning a value to a given name. In Python, dynamic typing eliminates the necessity of explicitly declaring data types.
 
x = 5
name = “John” 
 
4. Print Statement:
Within Python, the utilization of the print statement is aimed at showcasing output on the console. It is a built-in function that allows you to output text or values to the standard output.
 
Here’s a basic example:
 
print(“Hello, World!”)
 
 
5. Input:
Use input() to receive user input.
 
name = input(“Enter your name: “
 
6. Conditional Statements:
The conditional branching in programming is achieved through the utilization of if, elif, and else statements.
 
if x > 0:
    print(“Positive number”)
elif x < 0:
    print(“Negative number”)
else:
    print(“Zero”)
 
7. Loops:
for and while loops serve the purpose of iteration in Python.
 
for i in range(5):
print(i)
 
while x > 0:
print(x)
x -= 1
 
8. Functions:
Functions are defined using the def keyword.
 
def greet(name):
print(“Hello, “ + name + “!”)
 

 

Basic Data Types in Python: 

1. Integers: 
It encompasses integers, which can be either positive or negative whole numbers without any fractions or decimals.
 
age = 25
 
 2. Floats: 
It represents a real number through a floating-point notation. It is denoted by a decimal point.
 
height = 1.75
 
 3. Complex Numbers: 
Create a complex number by using the complex() function or by directly writing the real and imaginary parts.
 
# Using the complex() function 
x1 = complex(3, 4# 3 + 4j
 
# Directly writing real and imaginary parts
x2 = 1 + 2j
 
 4. Strings: 
A string constitutes a gathering of one or more characters encapsulated within single quotes, double quotes, or triple quotes.
 
message = “Hello, Python!”
 
5. Booleans: 
True and False with capital ‘T’ & ‘F’ are valid booleans otherwise python will throw an error.
 
  is_python_fun = True
 
 6. Lists: 
Lists are just like arrays, it can be created by just placing the sequence inside the square brackets[]. 
 
 fruits = [“apple”, “orange”, “banana”]
 
7. Tuples: 
Similar to a list, a tuple is an ordered assembly of Python objects. The sole distinction between a tuple and a list lies in the fact that tuples are immutable, meaning they cannot be altered after creation.
 
coordinates = (3, 4)
 
8. Dictionaries: 
A dictionary in Python is an unordered collection of data values, it holds a key: value pair. The inclusion of key-value pairs in a dictionary is implemented to enhance its optimization.
 
The student dictionary is initialized as follows: student = {“name”: “Alice”, “age”: 20, “grade”: “A”}
 
 9. Set:
A set is a collection of data types characterized by its lack of order, iterability, mutability, and absence of duplicate elements.
 
The student set is initialized as follows: fruits = {“apple”, “banana”, “kiwiberry”, “cherry”
 
These are the foundational elements of Python syntax and basic data types. 
 


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

Scroll to Top