-2

Let's say I have this python string

>>> s = 'dog /superdog/ | cat /thundercat/'

Is there a way to like replace the character / (first one) with [ & second / with ].

I was thinking like an about like this.

Output:

'dog [superdog] | cat [thundercat]'

I tried doing like this but did not quite get that well.

>>> s = 'dog /superdog/ | cat /thundercat/'
>>> s.replace('/','[')
'dog [superdog[ | cat [thundercat['

I was thinking to know the best and pythonic way as possible. Thank you!

Ice Bear
  • 2,676
  • 1
  • 8
  • 24
  • `and adds a million to it` - it doesn't. If it did, you would not be able to reach those numbers in your lifetime. – GSerg Oct 17 '20 at 14:04
  • Hello! Thank you but, No actually it was more on python, What I was trying to like wanted to know is for a computer itself, it doesn't matter of what programming language it is. – Ice Bear Oct 17 '20 at 14:04
  • I just made an example with python. – Ice Bear Oct 17 '20 at 14:05
  • 1
    It does matter what programming language you use because it's the rules of the programming language that decide what adding a number even is, and what happens when you perform it. – GSerg Oct 17 '20 at 14:07
  • By changing your code to add 9999999 instead of 1000000, you render responses harder to understand. – Zsolt Szilagyi Oct 17 '20 at 14:19
  • I changed it back to a million, I was just trying to figure out about the doubling sorry. Could you please let me finish up editing?. Thanks – Ice Bear Oct 17 '20 at 14:20
  • 1
    I have been reading the answer provided [here](https://stackoverflow.com/questions/5470693/python-number-limit) to be honest it's not quite the answer I am getting since I've also been reading answers here which are really good than that one on the link. So NO again it's not really the answer I am looking. Thank you! – Ice Bear Oct 17 '20 at 14:33

2 Answers2

2

Python can handle arbitrarily large numbers because python has built-in arbitrary-precision integers. The limit is related to the amount of RAM memory Python can access. These built-in Long Integers arithmetic is implemented as an Integer object which is initially set to 32 bits for speed, and then start allocating memory on demand.

Integers are commonly stored using a word of memory, which is 4 bytes or 32 bits, so integers from 0 up to 4,294,967,295 (2e32 -1) can be stored.

But if your system has 1GB available to a python process, it will have 8589934592 bits to represent numbers, and you can use numbers like (2e8589934592 -1).

  • Thank you for your answer! wow! I think I get your point may I ask what you mean by memory? is it the RAM right? just making sure... Also what do you mean too by `1GB bounded to a python process`? I have a `4GB RAM` here on my computer by the way :) – Ice Bear Oct 17 '20 at 14:30
  • essentially RAM memory, although you can use SWAP there your program will lose a lot of performance. – Alexandre Strapacao G. Vianna Oct 17 '20 at 14:37
  • Ohhhh... Thank you for your answer I think I am getting the essence you are referring to... So it all kinda depends on the RAM tho, I have a 4GB RAM and 64 bit OS. May I ask what do you mean by the 1GB bounded to a python process? I'm not sure what that means but I am curious of it. Thanks – Ice Bear Oct 17 '20 at 14:41
  • 1
    The correct word would be "available". I'll edit it. Usually, python has no memory limit, but your system may, for some reason, limit the amount of memory for a python process. – Alexandre Strapacao G. Vianna Oct 17 '20 at 14:47
  • Ohh Thank you! I am getting the point you are trying to make and will do more research on it. Unfortunately my question got "duplicated" I don't know why, I already said that the answer being suggested is not the answer I am looking for. In fact! your answer is good and much more direct to the one I am asking, It means that you understand what I really meant. But anyways! Thanks! – Ice Bear Oct 17 '20 at 14:49
0

Computers can only handle numbers up to a certain size, but this is to be taken with some caveats.

  • 2147483648 through 2147483647 are the limits of 32 bit numbers.
  • Most of todays computers can handle numbers of 64 bits, i.e. numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, or from −(2^63) to 2^63 − 1
  • It is possible to create a software that can handle arbitrary large numbers, as long as RAM or storage suffice. Those solutions are rather slow, but e.g. SSL encryption is based on numbers thousands of digits long.

As a side note, you are doubling your initial million in every iteration, not adding a million.

Zsolt Szilagyi
  • 4,741
  • 4
  • 28
  • 44
  • Also soo the limit also depends on the bit of the computer? About the doubling so I am not actually adding it, what proper way could I try to actually "`add`" a million? Thanks – Ice Bear Oct 17 '20 at 14:11
  • In the first iteration, you add 1 mio, but then you save that new value of 2 mio back into i. On the next iteration, you add 2 mio. Check your output, it's fairly obvious that it grows exponentially. – Zsolt Szilagyi Oct 17 '20 at 14:17
  • I changed the `i+=i` to `i = i + 1000000` so that adds up to a million right... I also noticed that it went a bit slower compared to the previous one – Ice Bear Oct 17 '20 at 14:22
  • 1
    "Computers can only handle numbers up to a certain size" – this seems misleading. Just as computers are free to interpret bit patterns as signed versus unsigned to increase the maximum value, they are free to interpret them differently to reach different maximum/minimum values. For example, a float can literally express an infinitely large number. – MisterMiyagi Oct 17 '20 at 14:36
  • 1
    @MisterMiyagi: well, even they max out, as only so many bits are there to describe the exponent. For a double-precision float (64 bits), that limit is around 10^1024 https://en.wikipedia.org/wiki/Double-precision_floating-point_format – Zsolt Szilagyi Oct 17 '20 at 14:48