cookie

Мы используем файлы cookie для улучшения сервиса. Нажав кнопку «Принять все», вы соглашаетесь с использованием cookies.

avatar

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.

Больше
Рекламные посты
7 090
Подписчики
Нет данных24 часа
Нет данных7 дней
Нет данных30 дней

Загрузка данных...

Прирост подписчиков

Загрузка данных...

If you want to contact us Message to this account For Ads and Freelancing Username: @Ping_TG
Показать все...
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
Показать все...
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
Показать все...
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
Показать все...
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
Показать все...
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
Показать все...
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
Показать все...
If you want to contact us Message to this account Username: @Ping_TG
Показать все...
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
Показать все...
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
Показать все...