0

Can someone explain this code?

import numpy as np ar = np.ones((3,2)) print(ar.ndim)

23rd Feb 2023, 8:49 AM
Lidwina Harefa
Lidwina Harefa - avatar
6 Answers
+ 8
S3R43o3 yes, you can do like that if this is your personal preference. but is there any benefit? i would say: NO. > readability lacks > no advantage in saving anything (space or time complexity) > not recommended by the PEP8 python style guide: *Compound statements (multiple statements on the same line) are generally discouraged* https://peps.python.org/pep-0008/
25th Feb 2023, 7:31 AM
Lothar
Lothar - avatar
+ 7
Lidwina Harefa the code as published is creating errors, since it should be in different lines: import numpy as np ar = np.ones((3,2)) print(ar.ndim)
23rd Feb 2023, 6:19 PM
Lothar
Lothar - avatar
+ 4
Sure! Let's go over this code line by line:import numpy as np This line imports the "numpy" library and gives it the name "np" to make it easier to reference later in the code. ar = np.ones((3,2)) This line creates a new NumPy array using the np.ones() function, which creates an array filled with ones. The argument to np.ones() is a tuple (3, 2) which specifies the shape of the array. In this case, it creates an array with 3 rows and 2 columns. print(ar.ndim) This line prints out the number of dimensions of the ar array. In this case, the array ar has 2 dimensions (rows and columns), so ar.ndim will return 2. So, when you run this code, it should output:2 which is the number of dimensions of the ar array.
23rd Feb 2023, 9:38 AM
Last
Last - avatar
0
Lothar or just write it like this import numpy as np; arr=np.ones((3,2));print(arr.ndim); âœŒïžđŸ˜‹
25th Feb 2023, 1:48 AM
S3R43o3
S3R43o3 - avatar
0
Sure your right well i used this way mostly if i create reverseshells or parsing through commandline, for pentesting. It was just a hint cause like you said its not common used. But beside this its nice to know
25th Feb 2023, 7:04 PM
S3R43o3
S3R43o3 - avatar
0
And believe me if you use some meterpretershells or some privilege escalation through zsh and the eof didnt work properly it save sooo many time 😁 or if you test some in the pythoncmd . You shouldn't use this in fully programms with 1000 + lines of code but for a fast web scrapping or many hacker stuff its useful
25th Feb 2023, 8:35 PM
S3R43o3
S3R43o3 - avatar