Featured Post

15 Python Tips : How to Write Code Effectively

Image
 Here are some Python tips to keep in mind that will help you write clean, efficient, and bug-free code.     Python Tips for Effective Coding 1. Code Readability and PEP 8  Always aim for clean and readable code by following PEP 8 guidelines.  Use meaningful variable names, avoid excessively long lines (stick to 79 characters), and organize imports properly. 2. Use List Comprehensions List comprehensions are concise and often faster than regular for-loops. Example: squares = [x**2 for x in range(10)] instead of creating an empty list and appending each square value. 3. Take Advantage of Python’s Built-in Libraries  Libraries like itertools, collections, math, and datetime provide powerful functions and data structures that can simplify your code.   For example, collections.Counter can quickly count elements in a list, and itertools.chain can flatten nested lists. 4. Use enumerate Instead of Range     When you need both the index and the value in a loop, enumerate is a more Pyth

How to Create an Array and Its Types in Python

Python supports various array types. Here is a list of arrays and how to create examples for your reference. 

What is an array?


Python array is a class that mimics the array type of other languages and offers efficiency in storage, exchanging that for flexibility.

How to create an array


Here is the syntax to create an array:

data = array('f', [12.8, 5.4, 8.0, 8.0, 9.21, 3.14])


It creates an array of 6 floating point numbers; the type is indicated by the 'f' as the first parameter to the constructor. This concept is unlike the Python norm of types being dynamic and malleable. An array is an array of one kind of thing, and an array can only hold a restricted set of types.


Array types

Types of arrays

Here is a list of arrays:

'b' A C++ char type
'B' A C++ unsigned char type
'i': A C++ int type
'l': A C++ long type
'f': A C++ float type
'd': A C++ double type

What is an array type?

Arrays are class objects and are provided in the built-in module array, which must be imported:

from array import array

An array is a sequence type and has the basic properties and operations that Python provides for all sequence types. Array elements can be assigned to and can be used in expressions, and arrays can be searched and extended like other sequences.

Tips: I have referred some best books and Udemy course. I am giving here for your ready reference. The best Udemy course is Python Bootcamp and the best Python book is Python pocket primer.
Ads

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Python placeholder '_' Perfect Way to Use it