Tumgik
#python array
data-science-lovers · 2 years
Text
youtube
Python Numpy Tutorials
0 notes
daemonhxckergrrl · 2 years
Text
just python things. this operation to reverse a number:
rev = int(str(num)[::-1])
is faster than this:
while num >= 1: rev = (rev * 10) + (num % 10) num = int(num/10)
for reversing an integer, because why wouldn't it be lmao
yeah let's just cast to string then chop backwards, and cast back to integer then save as a new variable. i suppose mod is a more expensive operation ??
but yeah it's like roughly 4x faster to use string comprehension.
103 notes · View notes
nintendont2502 · 4 months
Text
python save me. save me python
9 notes · View notes
watchmorecinema · 11 months
Text
Normally I just post about movies but I'm a software engineer by trade so I've got opinions on programming too.
Apparently it's a month of code or something because my dash is filled with people trying to learn Python. And that's great, because Python is a good language with a lot of support and job opportunities. I've just got some scattered thoughts that I thought I'd write down.
Python abstracts a number of useful concepts. It makes it easier to use, but it also means that if you don't understand the concepts then things might go wrong in ways you didn't expect. Memory management and pointer logic is so damn annoying, but you need to understand them. I learned these concepts by learning C++, hopefully there's an easier way these days.
Data structures and algorithms are the bread and butter of any real work (and they're pretty much all that come up in interviews) and they're language agnostic. If you don't know how to traverse a linked list, how to use recursion, what a hash map is for, etc. then you don't really know how to program. You'll pretty much never need to implement any of them from scratch, but you should know when to use them; think of them like building blocks in a Lego set.
Learning a new language is a hell of a lot easier after your first one. Going from Python to Java is mostly just syntax differences. Even "harder" languages like C++ mostly just mean more boilerplate while doing the same things. Learning a new spoken language in is hard, but learning a new programming language is generally closer to learning some new slang or a new accent. Lists in Python are called Vectors in C++, just like how french fries are called chips in London. If you know all the underlying concepts that are common to most programming languages then it's not a huge jump to a new one, at least if you're only doing all the most common stuff. (You will get tripped up by some of the minor differences though. Popping an item off of a stack in Python returns the element, but in Java it returns nothing. You have to read it with Top first. Definitely had a program fail due to that issue).
The above is not true for new paradigms. Python, C++ and Java are all iterative languages. You move to something functional like Haskell and you need a completely different way of thinking. Javascript (not in any way related to Java) has callbacks and I still don't quite have a good handle on them. Hardware languages like VHDL are all synchronous; every line of code in a program runs at the same time! That's a new way of thinking.
Python is stereotyped as a scripting language good only for glue programming or prototypes. It's excellent at those, but I've worked at a number of (successful) startups that all were Python on the backend. Python is robust enough and fast enough to be used for basically anything at this point, except maybe for embedded programming. If you do need the fastest speed possible then you can still drop in some raw C++ for the places you need it (one place I worked at had one very important piece of code in C++ because even milliseconds mattered there, but everything else was Python). The speed differences between Python and C++ are so much smaller these days that you only need them at the scale of the really big companies. It makes sense for Google to use C++ (and they use their own version of it to boot), but any company with less than 100 engineers is probably better off with Python in almost all cases. Honestly thought the best programming language is the one you like, and the one that you're good at.
Design patterns mostly don't matter. They really were only created to make up for language failures of C++; in the original design patterns book 17 of the 23 patterns were just core features of other contemporary languages like LISP. C++ was just really popular while also being kinda bad, so they were necessary. I don't think I've ever once thought about consciously using a design pattern since even before I graduated. Object oriented design is mostly in the same place. You'll use classes because it's a useful way to structure things but multiple inheritance and polymorphism and all the other terms you've learned really don't come into play too often and when they do you use the simplest possible form of them. Code should be simple and easy to understand so make it as simple as possible. As far as inheritance the most I'm willing to do is to have a class with abstract functions (i.e. classes where some functions are empty but are expected to be filled out by the child class) but even then there are usually good alternatives to this.
Related to the above: simple is best. Simple is elegant. If you solve a problem with 4000 lines of code using a bunch of esoteric data structures and language quirks, but someone else did it in 10 then I'll pick the 10. On the other hand a one liner function that requires a lot of unpacking, like a Python function with a bunch of nested lambdas, might be easier to read if you split it up a bit more. Time to read and understand the code is the most important metric, more important than runtime or memory use. You can optimize for the other two later if you have to, but simple has to prevail for the first pass otherwise it's going to be hard for other people to understand. In fact, it'll be hard for you to understand too when you come back to it 3 months later without any context.
Note that I've cut a few things for simplicity. For example: VHDL doesn't quite require every line to run at the same time, but it's still a major paradigm of the language that isn't present in most other languages.
Ok that was a lot to read. I guess I have more to say about programming than I thought. But the core ideas are: Python is pretty good, other languages don't need to be scary, learn your data structures and algorithms and above all keep your code simple and clean.
13 notes · View notes
relto · 10 months
Text
optimization journey: glue 10000+ arrays together for each data channel -> reduce number of array glueing required by doing 32 sequences at once -> NO array glueing at all!
3 notes · View notes
mytimeline1999 · 2 years
Text
Tumblr media
3 notes · View notes
Text
Tumblr media
1 note · View note
himejoshibutch · 3 days
Text
tech mutuals/followers i need some help...
i almost finished c programming so...
0 notes
thetexvn · 11 months
Text
Tumblr media
#Function and #array in #php
Read More: https://thetexvn.com/blogs/Function_and_Array_in_PHP
0 notes
data-science-lovers · 2 years
Text
Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media
Python Numpy Tutorials
0 notes
sad--tree · 1 year
Text
what do u do when u tell ur parent in no uncertain terms Thank You For The Offer But I Do Not Want A Tutor For This Course It Will Not Help And I Am Deeply Uncomfortable With It Do Not Get Me One
and then they go and book u with an online tutor. without asking. what the fuck.
0 notes
nicollis · 1 year
Text
Python 3.10 Bug
Just discovered a strange bug in Python. When you set a class initialization default parameter to an array, the array is in shared memory space. If initializing two objects one after another in code it is possible that their array will remain in shared memory space linking the two objects. python class Car(part_ids=[]) to fix don't do a default parameter instead detect if none and then create. python class Car(part_ids): self.part_ids = part_ids if part_ids is not None else [] Longer blog post on this coming soon.
1 note · View note
ivccseap · 2 years
Text
Arrays in Python
Arrays in Python
View On WordPress
0 notes
Text
python a* basic game pathfinding algorithm for navigation in a 2d array
from queue import PriorityQueue import math # A* pathfinding algorithm # https://pythonprogrammingsnippets.tumblr.com/ def astar(start, goal, terrain_map): frontier = PriorityQueue() frontier.put((0, start)) came_from = {} cost_so_far = {} came_from[start] = None cost_so_far[start] = 0 while not frontier.empty(): current = frontier.get()[1] if current == goal: break for next_tile in get_neighbors(current, terrain_map): new_cost = cost_so_far[current] + get_cost(current, next_tile, terrain_map) if next_tile not in cost_so_far or new_cost = 0 and y >= 0 and x < len(terrain_map) and y < len(terrain_map[0]): if (x, y) != tile and terrain_map[x][y] != 2: neighbors.append((x, y)) return neighbors def get_cost(current, next_tile, terrain_map): diagonal = abs(current[0] - next_tile[0]) == 1 and abs(current[1] - next_tile[1]) == 1 cost = terrain_map[next_tile[0]][next_tile[1]] if diagonal: cost *= math.sqrt(2) return cost ## ----------------------- def number_to_ascii_character(number): # # 1 = normal, 2 = wall, 3 = water, 4 = lava, 5 = ice, 6 = swamp if number == 1: return " " elif number == 2: return "-" # wall else: return "?" def show_map(terrain_map, path): for x in range(len(terrain_map)): for y in range(len(terrain_map[0])): if (x, y) in path: # if last position in path, show "X" if (x, y) == path[-1]: print("E", end="") # if first position in path, show "S" elif (x, y) == path[0]: print(" B ", end="") else: print(" x ", end="") else: map_char = number_to_ascii_character(terrain_map[x][y]) print(" " + map_char + " ", end="") print("") print("") print("") ## ----------------------- def main(): terrain_map = [ [1, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2 ], [1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2 ], [1, 2, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 1, 2 ], [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1 ], [1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1 ], [1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1 ], [2, 2, 1, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 1, 1, 2 ], [1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 2, 2 ], [1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 2, 2, 2 ], [1, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 2, 2 ], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 2, 2 ], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2 ], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1 ], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1 ], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1 ], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 2, 1 ], ] came_from, cost_so_far = astar((0, 0), (9, 9), terrain_map) current = (9, 9) path = [] while current != (0, 0): path.append(current) current = came_from[current] path.reverse() print("Start xy: (0, 0)") print("End xy: (9, 9)") print("-= | Path | =-") print(path) show_map(terrain_map, path) if __name__ == "__main__": main()
output:
Start xy: (0, 0) End xy: (9, 9) -= | Path | =- [(1, 0), (2, 0), (3, 0), (4, 1), (5, 2), (6, 2), (7, 2), (8, 2), (9, 3), (10, 4), (10, 5), (9, 6), (8, 6), (7, 7), (8, 8), (9, 9)] - - - - - - - B - - - - - - - x - - - - - - - - - x - - - - - - x - - - - - x - - - - - - - - x - - - - - - x - - - x - - - - - x - - x - x - - - - - x - - x - E - - - - - - x x - - - - - - - - - - - - - - - - - - - - - - - -
0 notes
itonlinetraining · 2 years
Text
An array in Python is simply a collection of data in a particular data type. Lists in some sense can be seen as arrays, only that the elements of a list can contain more than one data type. Arrays do not support multiple data types. In Python, arrays are created using the array module which must be specifically imported. 
Arrays are used for mathematical computations so their elements must be a numerical data type. In this tutorial, we will discuss how to create an array in Python and the various operations that can be done on an array. In specific terms, here’s what you will learn by the end of this tutorial. 
0 notes
Text
Tumblr media
C language MCQ . . . . write your answer in the comment section https://bit.ly/48J8x0O You can check the answer at the above link at Q.no. 1
0 notes