NumPy: Create an array with 10^3 elements
NumPy: Array Object Exercise-69 with Solution
Create Array with 10^3 Elements
Write a NumPy program to create an array with 10^3 elements.
Sample Solution:
Python Code:
# Importing the NumPy library and aliasing it as 'np'
import numpy as np
# Creating an array 'x' using NumPy's arange function to generate numbers from 0 to 999 (inclusive)
x = np.arange(1e3)
# Printing the array 'x' containing numbers from 0 to 999
print(x)
Sample Output:
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11 . 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23 . 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35 . 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47 . 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59 . 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71 . 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83 . 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95 .............................. 900. 901. 902. 903. 904. 905. 906. 907. 908. 909. 910. 911 . 912. 913. 914. 915. 916. 917. 918. 919. 920. 921. 922. 923 . 924. 925. 926. 927. 928. 929. 930. 931. 932. 933. 934. 935 . 936. 937. 938. 939. 940. 941. 942. 943. 944. 945. 946. 947 . 948. 949. 950. 951. 952. 953. 954. 955. 956. 957. 958. 959 . 960. 961. 962. 963. 964. 965. 966. 967. 968. 969. 970. 971 . 972. 973. 974. 975. 976. 977. 978. 979. 980. 981. 982. 983 . 984. 985. 986. 987. 988. 989. 990. 991. 992. 993. 994. 995 . 996. 997. 998. 999.]
Explanation:
The above code creates and prints a NumPy array with 1000 elements, ranging from 0 to 999.
x = np.arange(1e3): This line creates a 1D NumPy array with elements from 0 to 999 (1e3 is scientific notation for 1000). The np.arange() function generates an array of evenly spaced values within the specified range.
print(x): This line prints the created NumPy array ‘x’ containing the values from 0 to 999.
Python-Numpy Code Editor:
Previous: Write a NumPy program (using numpy) to sum of all the multiples of 3 or 5 below 100.
Next: Write a NumPy program to create and display every element of an numpy array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-69.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics