Thursday, 16 July 2026

Python Coding challenge - Day 1212| What is the output of the following Python Code?

 


Code Explanation:

๐Ÿ”น 1. Importing the array Class
from array import array
✅ Explanation
array is imported from Python's built-in array module.
Unlike a Python list, an array stores only one type of data.
Arrays are more memory-efficient than lists when storing many numbers.

Think of an array as a train where every compartment must carry the same type of passenger.

Python List


Can Store

1
"Python"
5.5

Array


Can Store

Only One Data Type

๐Ÿ”น 2. Creating an Integer Array
nums = array('i', [1, 2, 3])
✅ Explanation

Here Python creates an array.

Syntax:

array(typecode, iterable)

There are two parts:

Type Code
'i'

means

Signed Integer

Common type codes:

Type Code Meaning
'i' Integer
'f' Float
'd' Double
'u' Unicode Character

The second argument is

[1, 2, 3]

These values are copied into the array.

Current memory:

nums


array('i',[1,2,3])

๐Ÿ”น 3. Understanding the Array

Current array:

Index

0   1   2


1   2   3

Unlike a list,

array('i')

cannot store:

"Hello"

or

5.5

because every element must be an integer.

๐Ÿ”น 4. Appending a New Value
nums.append(4)
✅ Explanation

append() adds a new element at the end of the array.

Before:

array('i')


1

2

3

After appending:

array('i')


1

2

3

4

Current memory:

nums


array('i',[1,2,3,4])

๐Ÿ”น 5. Calling tolist()
nums.tolist()
✅ Explanation

An array object is not a normal Python list.

The tolist() method converts the array into a standard Python list.

Before conversion:

array


array('i',[1,2,3,4])

After conversion:

List


[1,2,3,4]

No values change—only the data structure changes.

๐Ÿ”น 6. Printing the List
print(nums.tolist())
✅ Explanation

Python prints the converted list.

Output:

[1, 2, 3, 4]

๐ŸŽฏ Final Output
[1, 2, 3, 4]

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (310) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (282) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (9) Data Analysis (40) Data Analytics (27) data management (16) Data Science (394) Data Strucures (23) Deep Learning (200) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (23) Finance (11) flask (4) flutter (1) FPL (17) Generative AI (76) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (350) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1404) Python Coding Challenge (1191) Python Mathematics (5) Python Mistakes (51) Python Quiz (572) Python Tips (25) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)