Posts

Showing posts from September, 2023

Slicing in Python

Image
 You can slice pretty much any sequence in Python. A sequence is something that you can index from 0 to len(sequence)-1. Lists, tuples, and strings are all examples of sequences. Syntax: sequence[start:stop:step]   Let say we have a variable fruits that points to a list:   If we put a colon and another number inside the square brackets, we're slicing this list instead of indexing it: Indexes start from 0. for example, from "Hello, World!" we need to extract "world" for that use sequence[7,12] as shown below. In the second example, we have given step size as 2, so it will check for 2 step size. The output displayed as [1,3]

Sets in Python

Image
A set is an unordered collection of elements without any duplicates. Sets are especially useful for performing logical operations like finding the union, intersection, or difference between collections of elements. For example, sets could be used to determine mutual friends on a social networking site. Syntax : x = set(iterables) or x = {value1,value2,value3......valuen} There are several ways to create a set, which include: Using the built-in  set()  function and passing in an optional iterable parameter. Hard-coding a set with dictionary-like syntax ({}) where each element is unique.   Here, it is not printed the GCP name twice, It is a duplicate record & it will only display the Unique records. The other methods in the sets defined below .difference(): Returns a new set of objects unique to a given set when compared to others. .intersection(): Returns a new set with objects that exist inside two or more sets .union(): Returns a new...

Dictionaries in Python

Image
A dictionary is a data set of key-value pairs. It provides a way to map pieces of data to each other and allows for quick access to values associated with keys. In Python, dictionaries are dynamic and mutable, which means they can change. Note:  As of Python version 3.7, dictionaries are ordered based on insertion, but this is not the case in previous versions.   Syntax: dictionary_name = { key1: value1,   key2: value2,   key3: value3 } Each entry in a dictionary is a key-value pair. Each pair is separated by a comma. Dictionary keys must be immutable types such as numbers and strings because keys should not change. Keys cannot be lists because lists are mutable, and it will raise a TypeError. Values can be any type, such as strings, numbers, lists, and even other dictionaries. Dictionary Creation: 1. dic = {} 2.x = dict() A same dictionary can be created as below: t1 = {1:'python',2:'Java',3:'Hadoop','4:'Aws'} Here {1,2,3} are keys and {...

Datatypes in Python

Image
Python is a strongly typed language, in the sense that at runtime it prevents typing errors and it engages in little implicit type conversion or casting, i.e. converting one type to another without a specific call to a conversion function. Python includes the following categories of built-in data types: String type: str Boolean type: bool Binary types: bytes, bytearray, memoryview Number types: int, float, complex Sequence Types: list, range, tuple Set types: set, frozenset Dictionary type: dict   type() The type() function can be used to retrieve the data type of an object: isinstance() The isinstance() function can be used to test if an object is an instance of a specified type. This will print a boolean value for each function call, indicating if the object is an instance of the given type: