Python Codes @python_codes Channel on Telegram

Python Codes

@python_codes


This channel will serve you all the codes and programs which are related to Python.

We post the codes from the beginner level to advanced level.

Python Codes (English)

Are you a Python enthusiast looking to enhance your coding skills? Look no further than our Telegram channel, @python_codes! This channel is dedicated to providing you with a wide range of Python codes and programs, from beginner level to advanced level.

Whether you are new to Python or an experienced coder looking to sharpen your skills, our channel is the perfect resource for you. We carefully curate and post codes that cover a variety of topics, including data analysis, web development, machine learning, and more. No matter what your interests are within the realm of Python programming, you'll find valuable content on our channel.

Join our growing community of Python enthusiasts and stay updated with the latest trends and techniques in Python programming. Our goal is to help you learn, grow, and achieve success in your coding journey. Don't miss out on the opportunity to expand your Python knowledge and skills - join @python_codes today!

Python Codes

24 Dec, 06:30


NumPy tricks for beginners :

๐Ÿ‘‰ Reshaping arrays: NumPy provides the np.reshape() function, which allows you to change the shape of an array while preserving its data. This can be useful for converting between different data formats, such as converting a one-dimensional array into a two-dimensional matrix. For example, the following code reshapes a one-dimensional array into a two-dimensional matrix with two rows and three columns:

import numpy as np

# Create a one-dimensional NumPy array
x = np.array([1, 2, 3, 4, 5, 6])

# Reshape the array into a two-dimensional matrix with 2 rows and 3 columns
x_matrix = np.reshape(x, (2, 3))

# Print the resulting matrix
print(x_matrix)

output:
[[1 2 3]
[4 5 6]]

๐Ÿ‘‰Stacking arrays: NumPy provides the np.vstack() and np.hstack() functions, which allow you to stack arrays vertically or horizontally. This can be useful for combining multiple arrays into a single array, or for splitting a single array into multiple arrays. For example, the following code stacks two one-dimensional arrays vertically to create a two-dimensional matrix:

import numpy as np

# Create two one-dimensional NumPy arrays
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

# Stack the arrays vertically to create a two-dimensional matrix
z = np.vstack((x, y))

# Print the resulting matrix
print(z)

output:
[[1 2 3]
[4 5 6]]

๐Ÿ‘‰Broadcasting: NumPy allows you to perform mathematical operations on arrays with different shapes, using a technique called broadcasting. This allows you to perform operations on arrays of different sizes, as long as their shapes are compatible. For example, the following code adds a scalar value to each element of a two-dimensional array:

import numpy as np

# Create a two-dimensional NumPy array
x = np.array([[1, 2, 3],
[4, 5, 6]])

# Add a scalar value to each element of the array
y = x + 10

# Print the resulting array
print(y)

output:
[[11 12 13]
[14 15 16]]

Share and Support
@Python_Codes

Python Codes

22 Dec, 06:29


Basic NumPy for beginners:

Creating a NumPy array: To create a NumPy array from a list or tuple, you can use the np.array() function. For example, the following code creates a NumPy array from a list of numbers:

import numpy as np

# Create a NumPy array from a list of numbers
numbers = [1, 2, 3, 4, 5]
numbers_array = np.array(numbers)

# Print the array
print(numbers_array)

output:
[1 2 3 4 5]

Basic mathematical operations: NumPy provides functions for performing mathematical operations on arrays, such as addition, subtraction, multiplication, and division. These operations can be performed element-wise, allowing for efficient computation on large datasets. For example, the following code adds two NumPy arrays element-wise:

import numpy as np

# Create two NumPy arrays
x = np.array([1, 2, 3, 4, 5])
y = np.array([6, 7, 8, 9, 10])

# Add the arrays element-wise
z = x + y

# Print the result
print(z)

output:
[ 7 9 11 13 15]

Indexing and slicing: NumPy arrays can be indexed and sliced just like lists. This allows you to access and manipulate specific elements or subarrays within an array. For example, the following code slices a NumPy array to extract the second and third elements:

import numpy as np

# Create a NumPy array
numbers = np.array([1, 2, 3, 4, 5])

# Slice the array to extract the second and third elements
subarray = numbers[1:3]

# Print the result
print(subarray)

output:
[2 3]

Share and Support
@Python_Codes

Python Codes

20 Dec, 06:30


NumPy is a library for scientific computing in Python. It provides tools for working with arrays of data, including functions for mathematical operations, linear algebra, and random number generation.

๐Ÿ‘‰๐ŸปOne of the key features of NumPy is its array data structure, which is similar to a list but allows for more efficient mathematical operations on large datasets. NumPy arrays can be created from existing data, such as lists or tuples, using the np.array() function.

๐Ÿ‘‰๐ŸปOnce an array has been created, it can be manipulated using various NumPy functions. For example, the np.mean() function can be used to compute the mean of an array, and the np.random.rand() function can be used to generate random numbers.

๐Ÿ‘‰๐ŸปIn addition to its array data structure, NumPy also provides a wide range of mathematical functions for working with arrays, such as linear algebra operations, statistical functions, and trigonometric functions. These functions can be applied to arrays element-wise, allowing for efficient computation on large datasets.

Overall, NumPy is a powerful library for working with arrays of data in Python, and is widely used in the fields of scientific computing, data science, and machine learning.

Share and Support
@Python_Codes

Python Codes

18 Dec, 06:30


Commonly used Python libraries are:

๐Ÿ‘‰๐ŸปNumPy:
This library is used for scientific computing and working with arrays of data. It provides functions for working with arrays of data, including mathematical operations, linear algebra, and random number generation.

๐Ÿ‘‰๐ŸปPandas: This library is used for data manipulation and analysis. It provides tools for importing, cleaning, and transforming data, as well as tools for working with time series data and performing statistical analysis.

๐Ÿ‘‰๐ŸปMatplotlib: This library is used for data visualization. It provides functions for creating a wide range of plots, including scatter plots, line plots, bar plots, and histograms.

๐Ÿ‘‰๐ŸปScikit-learn: This library is used for machine learning. It provides a range of algorithms for classification, regression, clustering, and dimensionality reduction, as well as tools for model evaluation and selection.

๐Ÿ‘‰๐ŸปTensorFlow: This library is used for deep learning. It provides a range of tools and libraries for building and training neural networks, including support for distributed training and hardware acceleration.

Share and Support
@Python_Codes

Python Codes

16 Dec, 06:31


common techniques for using the type() function in Python:

Get the type of an object:

my_object = "Hello, world!"
my_type = type(my_object)


Check if an object is of a given type:

my_object = "Hello, world!"
if type(my_object) == str:
print("my_object is a string.")


Create a new object of a given type:

my_type = int
my_object = my_type("1")


Use the type of an object as a dictionary key:

my_object = "Hello, world!"
my_type = type(my_object)
my_dict = {}
my_dict[my_type] = my_object


Use the type of an object as a set element:

my_object = "Hello, world!"
my_type = type(my_object)
my_set = set()
my_set.add(my_type)


The type() function is a built-in function in Python that is used to determine the type of an object. It is a useful tool for checking the type of an object, creating objects of a specific type, and using the type of an object as a key in a dictionary or set.

Share and Support
@Python_Codes

Python Codes

14 Dec, 06:30


Common techniques for using the repr() function in Python:

Get the string representation of an object:

my_object = "Hello, world!"
my_repr = repr(my_object)


Print the string representation of an object:
  
my_object = "Hello, world!"
print(repr(my_object))


Use the string representation of an object for debugging:
  
my_object = "Hello, world!"
print(f"my_object: {repr(my_object)}")


Use the string representation of an object as a dictionary key:
  
my_object = "Hello, world!"
my_repr = repr(my_object)
my_dict = {}
my_dict[my_repr] = my_object


Use the string representation of an object as a set element:

my_object = "Hello, world!"
my_repr = repr(my_object)
my_set = set()
my_set.add(my_repr)


Keep in mind that the repr() function is used to generate a string representation of an object that is meant to be readable and unambiguous. The resulting string may not be the same as the original value of the object, and it may not be suitable for use in some contexts.


Share and Support
@Python_Codes

Python Codes

12 Dec, 06:30


Common techniques for using the hash() function in Python:

Get the hash value of an object:

my_object = "Hello, world!"
my_hash = hash(my_object)



Hash multiple objects:

my_object1 = "Hello, world!"
my_object2 = (1, 2, 3)
my_object3 = {"key": "value"}
my_hash1 = hash(my_object1)
my_hash2 = hash(my_object2)
my_hash3 = hash(my_object3)



Use the hash value for an object as a dictionary key:

my_object = "Hello, world!"
my_hash = hash(my_object)
my_dict = {}
my_dict[my_hash] = my_object


Use the hash value for an object as a set element:

my_object = "Hello, world!"
my_hash = hash(my_object)
my_set = set()
my_set.add(my_hash)

Keep in mind that the hash() function is used to generate a numeric value that represents the value of an object. The value of the hash may change between different runs of a Python program, so it should not be used as a unique identifier for objects unless you are sure that the hash will not change.

In addition, not all objects are hashable, so it is not possible to use the hash() function with every type of object in Python.


Share and Support
@Python_Codes

Python Codes

11 Dec, 09:04


If you want to contact us

Message to this account

Username: @Ping_TG

Python Codes

10 Dec, 06:30


In general, the Python standard library includes many built-in functions that are available to use in your code without needing to import any additional modules. Some common examples of built-in functions include:

๐Ÿ‘‰๐Ÿป abs() :
Returns the absolute value of a number.

๐Ÿ‘‰๐Ÿป all() : Returns True if all elements of an iterable are True, and False otherwise.

๐Ÿ‘‰๐Ÿป any() : Returns True if any element of an iterable is True, and False otherwise.

๐Ÿ‘‰๐Ÿป bin() : Converts an integer to a binary string.

๐Ÿ‘‰๐Ÿป bool() : Converts a value to a Boolean.

๐Ÿ‘‰๐Ÿป chr() : Returns the string representation of a Unicode character.

๐Ÿ‘‰๐Ÿป dir() : Returns a list of attributes and methods for an object.

๐Ÿ‘‰๐Ÿปenumerate(): Returns an enumerate object, which contains a sequence of tuples containing the index and value of each element of an iterable.

๐Ÿ‘‰๐Ÿป filter() : Returns an iterator for elements of an iterable for which a condition is True.

๐Ÿ‘‰๐Ÿป float() : Converts a value to a floating-point number.

๐Ÿ‘‰๐Ÿป format(): Formats a string using format specifiers.

๐Ÿ‘‰๐Ÿป hash() : Returns the hash value of an object.

๐Ÿ‘‰๐Ÿป int() : Converts a value to an integer.

๐Ÿ‘‰๐Ÿป isinstance(): Returns True if an object is an instance of a given type, and False otherwise.

๐Ÿ‘‰๐Ÿป len() : Returns the length of an object.

๐Ÿ‘‰๐Ÿป list() : Converts an iterable to a list.

๐Ÿ‘‰๐Ÿป map() : Returns an iterator that applies a function to each element of an iterable.

๐Ÿ‘‰๐Ÿป max() : Returns the maximum value of an iterable.

๐Ÿ‘‰๐Ÿป min() : Returns the minimum value of an iterable.

๐Ÿ‘‰๐Ÿป next() : Returns the next element of an iterator.

๐Ÿ‘‰๐Ÿป open() : Opens a file and returns a file object.

๐Ÿ‘‰๐Ÿป ord() : Returns the Unicode code point for a character.

๐Ÿ‘‰๐Ÿป print() : Prints a message to the standard output.

๐Ÿ‘‰๐Ÿป range() : Returns a sequence of numbers.

๐Ÿ‘‰๐Ÿป repr() : Returns a string representation of an object.

๐Ÿ‘‰๐Ÿป round() : Rounds a number to a specified number of decimal places.

๐Ÿ‘‰๐Ÿป set() : Creates a set object.

๐Ÿ‘‰๐Ÿป sorted() : Returns a sorted list from an iterable.

๐Ÿ‘‰๐Ÿป str() : Converts a value to a string.

๐Ÿ‘‰๐Ÿป sum() : Returns the sum of elements in an iterable.

๐Ÿ‘‰๐Ÿป type() : Returns the type of an object.

๐Ÿ‘‰๐Ÿป zip() : Returns an iterator that combines elements from multiple iterables.

Share and Support
@Python_Codes

Python Codes

08 Dec, 06:30


The union of two sets can be found using the union() method.

Here is an example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)

This will create a new set called union_set that contains all of the items from set1 and set2. In this case, union_set will be equal to {1, 2, 3, 4, 5}.

You can also use the | operator to find the union of two sets. For example:


set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2

This will produce the same result as the union() method.

Keep in mind that sets are unordered collections of unique items, so the order of the items in the union set may not be the same as the order of the items in the original sets.

Share and Support
@Python_Codes

Python Codes

06 Dec, 06:30


Common techniques for using the zip() function in Python:

1. Zip two lists together:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
zipped_lists = zip(list1, list2)


2. Unzip a zipped list:

zipped_lists = [(1, 4), (2, 5), (3, 6)]
list1, list2 = zip(*zipped_lists)


3. Loop through a zipped list:

zipped_lists = [(1, 4), (2, 5), (3, 6)]
for item1, item2 in zipped_lists:
print(item1, item2)


4.Convert a zipped list to a dictionary:

zipped_lists = [(1, 4), (2, 5), (3, 6)]
my_dict = dict(zipped_lists)


5. Zip three or more lists together:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
zipped_lists = zip(list1, list2, list3)


Share and Support
@Python_Codes

Python Codes

04 Dec, 06:31


Common techniques for using the set() function in Python:

1. Create a set from a list:

my_list = [1, 2, 2, 3, 3, 3]
my_set = set(my_list)


2.Add an item to a set:

my_set = {1, 2, 3}
my_set.add(4)


3.Remove an item from a set by its value:

my_set = {1, 2, 3}
my_set.remove(3)


4.Check if an item is in a set:

my_set = {1, 2, 3}
if 3 in my_set:
print("The item is in the set.")


5.Get the length of a set:

my_set = {1, 2, 3}
set_length = len(my_set)


6.Loop through the items in a set:

my_set = {1, 2, 3}
for item in my_set:
print(item)


7.Get the union of two sets:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)


8.Get the intersection of two sets:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)

Share and Support
@Python_Codes

Python Codes

02 Dec, 05:41


๐Ÿ’ก๐Ÿ Did you know?

You can use slices in Python to insert elements to a list in arbitrary positions?


Share and Support
@Python_Codes

Python Codes

12 Nov, 06:31


What are pure functions?

A pure function is a function that will return the same value given the same arguments. A function is not pure if there is something outside the function that can change its return value, given the same arguments.


Share and Support
@Python_Codes

Python Codes

11 Nov, 06:30


Do you want to know the recursion limit in Python?


Share and Support
@Python_Codes

Python Codes

10 Nov, 06:30


How do you check if an array is monotonic?

An array is monotonic if the elements are sorted either in descending order or ascending order.

Share and Support
@Python_Codes

Python Codes

15 Oct, 06:30


round() function

the round function is used to convert the floating-point into a specified number of decimals number

@Python_Codes

Python Codes

14 Oct, 06:31


any() function returns True if at least one item is True in the iterable object.

@Python_Codes

Python Codes

13 Oct, 06:30


Most of the time, we don't want to share our wifi passwords, in that case, we generate a QR and stick it on the wall.

@Python_Codes

Python Codes

07 Oct, 06:30


How to convert a CSV file to MS Excel with Python and OpenPyXL

@Python_Codes