BLOG on “PYTHON”

Vishvaasswaminathan
6 min readApr 23, 2023

--

Python is a general-purpose programming language that is becoming ever more popular for data science, to learn a python we need to start by knowing what are the data types in python.

What is the data type?

Variables are used to hold values of different data types. Python is called dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value
with its type. Python enables us to see the sort of variable utilized in the program. Python provides us the sort () function which returns the type of the variable passed.

Python provides various standard data types that outline the storage method on each of them.

Data types in Python.

  1. Numbers (Integer)
  2. String
  3. List
  4. Tuple
  5. Dictionary

Number(Integer)

A number is used to store numeric values. Python creates Number objects when variety is assigned to a variable.

For example:

a = 10,
b = 20

#Here a and b are number objects in which we pass 10and 20

Python supports 4 types of numeric data.

NUMERIC(integer)

(signed integers like 13, 2, 29, etc.)

FLOAT

(float is used for floating-point numbers like 1.9, 9.902, 15.2, etc.)

COMPLEX

(complex numbers like 2.14j, 2.0 + 2.3j, etc.)

Important note

  • Python allows us to use a lower-case L to be used with long integers.
  • However, we should use an upper-case L to avoid confusion.
  • A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts respectively).

String

The string can be defined as the sequence of characters represented in the quotation(“ ”) marks. We will use single, double, or triple quotes to define a string in Python.

Single Quote string:

String handling in python is a straight forward task since there are various inbuilt functions and operators provided.

For example:

a = ‘HELLO INNOMATICS’

b = “HELLO INNOMATICS”

c = ‘’’HELLO INNOMATICS’’’

LIST

Lists are similar to arrays in C. However; the list can contain data of various types. The items stored in the list are separated with a comma (,) and enclosed within square brackets []. We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Example:

x = [1,2]
type(x),len(x)

o/p: (list, 2)

What Is Slicing?

Slicing is the extraction of a part of a string, list, or tuple. It enables users to access the specific range of elements by mentioning their indices.

Syntax: var[start_index:end_index:step_index]

  • “Start” specifies the starting index of a slice
  • “Stop” specifies the ending element of a slice
  • You can use one of these if you want to skip certain items

Definition and Usage of slicing:-

The slice() function returns a slice object.

A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.

Example:-

Create a tuple and a slice object. Use the step parameter to return every third item:

a = (“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”)
x = slice(0, 8, 3)
print(a[x])

Slice deletion in Python

Users can delete multiple items out of the data structure by using a del statement.

Example:-

Note: Tuple object does not support item deletion.

BASIC RULES OF SLICING in PYTHON:

a[start:stop]  # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole array
a[-1]    # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items
a[::-1]    # all items in the array, reversed
a[1::-1] # the first two items, reversed
a[:-3:-1] # the last two items, reversed
a[-3::-1] # everything except the last two items, reversed
a[slice(start, stop, step)]
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1

Indexing

“Indexing” means referring to an element of an iterable by its position within the iterable. “Slicing” means getting a subset of elements from an iterable based on their indices.

Conditional Statements in Python

A conditional statement as the name suggests itself, is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions encountered by the program.

Python has 3 key Conditional Statements that you should know:

  • if statement
  • if-else statement
  • if-elif-else ladder

if Statement:

The if statement is a conditional statement in python, that is used to determine whether a block of code will be executed or not. Meaning if the program finds the condition defined in the if statement to be true, it will go ahead and execute the code block inside the if statement.

Syntax:

if condition:
# execute code block

if-else Statement:

As discussed above, the if statement executes the code block when the condition is true. Similarly, the else statement works in conjuncture with the if statement to execute a code block when the defined if condition is false.

Syntax:

Syntax:
if condition:
# execute code if condition is true
else:
# execute code if condition if False

if-elif-else ladder:

The elif statement is used to check for multiple conditions and execute the code block within if any of the conditions evaluate to be true.

The elif statement is similar to the else statement in the context that it is optional but unlike the else statement, there can be multiple elif statements in a code block following an if statement.

if condition1:
# execute this statement
elif condition2:
# execute this statement
.
.
else:
# if non of the above conditions
# evaluate to True
# execute this statement

Nested if Statements:

A nested if statement is considered as if within another if statement(s). These are generally used to check for multiple conditions.

Syntax:

if condition1:
if condition2:
# execute code if both condition1
# and condition2 are True

Python if…else Flowchart

SETS{}

  1. Uniqueness: Sets in Python are collections of unique elements, which means that no duplicate values are allowed. If you try to add a duplicate element to a set, it will not be added and the set will remain unchanged.
  2. Mutable: Sets are mutable, which means that you can add or remove elements from a set after it has been created. This allows you to modify the contents of a set as needed.
  3. Unordered: Sets are unordered collections, which means that the elements in a set have no specific order. The order of elements in a set may change over time, and you cannot access elements in a set by index like you can in a list.
  4. Hashable Elements: Elements in a set must be hashable, which means that they must have a unique hash value that is used to determine their position in the set. Immutable data types like numbers, strings, and tuples are hashable and can be used as elements in a set, while mutable data types like lists and dictionaries are not hashable and cannot be used in sets.
  5. Set Operations: Sets support a variety of set operations, which are performed using built-in methods. Some common set operations include:
  • Union: union() or | returns a new set containing all the elements from two or more sets.
  • Intersection: intersection() or & returns a new set containing only the elements that are common to two or more sets.
  • Difference: difference() or - returns a new set containing the elements that are in one set but not in another.
  • Symmetric Difference: symmetric_difference() or ^ returns a new set containing the elements that are in exactly one of two sets.
  • Subset: issubset() or <= checks if one set is a subset of another set.
  • Superset: issuperset() or >= checks if one set is a superset of another set.
  1. Set Membership: You can check if an element is present in a set using the in keyword, which returns a boolean value. For example:
pythonCopy code
my_set = {1, 2, 3, 4, 5}
if 3 in my_set:
print("3 is in the set.")

--

--

No responses yet