As others have said, 32-bit versions of numpy still support 64-bit dtypes. But if you really need to convert to int32, you can use the astype function:

>>> import numpy as np
>>> x = np.array([1,2,3], dtype=np.int64)
>>> x
array([1, 2, 3])
>>> x.dtype
dtype('int64')
>>> y = x.astype(np.int32)
>>> y
array([1, 2, 3], dtype=int32)
Answer from jterrace on Stack Overflow
Top answer
1 of 2
4

If the result should be an integer, you should use operator.floordiv, i.e. a // b, not a / b 1:

from operator import add, sub, mul, floordiv, mod

op = {'+': add, '-': sub, '*': mul, '/': floordiv, '%': mod}

for i in range(int(input())):
    znak, *nums = input().split()

    print(opznak))

By the way, the code above does exactly the same as yours, but it's around two times shorter!

How?

  • why check if znak is equal to something with tons of if/else statements, if you could put all the math operators in a dictionary op, and get the operator you need with op[znak]?
  • you're on Python 3, so you can use that nice a, *b = iterable syntax which extracts the first item of iterable into a and puts other items into a list b, which looks super nice
  • each op[znak] is a function that accepts two arguments, so you convert nums to integers with map(int, nums) and then pass them as separate arguments with the asterisk: *map(int, nums)
  • last, but not least, why use this C-style while loop index incrementing if there's the Pythonic way to do it - with range(start, stop, [step])?
  • finally, you don't really need the variable n, so you can plug it into range right away

Pssst, dude, feeling in need of some craziness? Take a look at how you can squeeze all this into two lines:

from operator import*

sum(0for _ in map(print,((lambda znak,*nums:{'+':add,'-':sub,'*':mul,'/':floordiv,'%':mod}znak))(*input().split())for _ in range(int(input())))))

Or only one line:

sum(0for _ in map(print,((lambda znak,*nums:{'+':lambda a,b:a+b,'-':lambda a,b:a-b,'*':lambda a,b:a*b,'/':lambda a,b:a//b,'%':lambda a,b:a%b}znak))(*input().split())for _ in range(int(input())))))

These work exactly as the first version. Now, this is just for fun, to show how powerful Python is and how messy it can be. Don't try this at home :D


1 True division vs Floor division: 1 / 10 == 0.1, but 1 // 10 == 0.

2 of 2
0

A simple solution: The idea is to split input charter into tokens preserving their orders . in our case tokens are operators and numbers so it very simple using regular expressions. [+-/*] match any of those operators or any number of digits using \d+

n = input()
import re
while n>0:
    expression = input()
    operator,num1,num2 = re.findall('[+-/*]|\d+',expression)
    if operator == '+': print(int(num1) + int (num2))
    if operator == '-': print(int(num1) - int (num2))
    if operator == '*': print(int(num1) * int (num2))
    if operator == '%': print(int(num1) % int (num2))
    if operator == '/': print(int(num1) // int (num2))

    n = n - 1
Discussions

python - How to convert signed 32-bit int to unsigned 32-bit int? - Stack Overflow
import ctypes def int32_to_uint32(i): return ctypes.c_uint32(i).value ... Sign up to request clarification or add additional context in comments. ... That returns a Python integer type, though, which probably isn't a meaningful result in this context, as it isn't actually a uint32 anymore. More on stackoverflow.com
🌐 stackoverflow.com
[beginner] How to best convert a 64bit integer to a 32 bit float
Answer for the question in the title: from numpy import float64, float32 num = float64(4.) print(num, type(num)) num = float32(num) print(num, type(num)) Answer based on your post: Have you considered using round? It returns a Python built in integer if used with a single argument. More on reddit.com
🌐 r/learnpython
5
1
August 9, 2022
Combine byte values to a 32bib int
I get 4 8bit (1 Byte) values from a i.e. serial input. In fact it is unix time in pieces, but to work with it I need it in one 32 bit int · Whether it’s a signed ('i' / int32 ) or unsigned('I' / uint32) integer was not specified, but try: · Source code: Lib/struct.py This module converts ... More on discuss.python.org
🌐 discuss.python.org
4
0
January 20, 2025
Converting hex to uint32
Without any example data or code the only thing I can say is use the struct module. More on reddit.com
🌐 r/learnpython
4
1
July 17, 2024
People also ask

How do you convert a list of integers to a single string in Python?

You can use the join() method combined with map() to convert each integer to a string and concatenate them.

int_list = [1, 2, 3, 4]
result = ''.join(map(str, int_list))
print(result)  # Output: '1234'
🌐
datacamp.com
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
What happens if you try to convert a non-numeric string to an integer in Python?

A ValueError will be raised indicating that the string is not a valid integer.

int('abc')  # Raises ValueError: invalid literal for int() with base 10: 'abc'
🌐
datacamp.com
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
Can you convert a set to a list in Python? How?

Yes, you can convert a set to a list using the list() function.

a_set = {1, 2, 3}
a_list = list(a_set)
print(a_list)  # Output: [1, 2, 3]
🌐
datacamp.com
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
🌐
Real Python
realpython.com › convert-python-string-to-int
How to Convert a Python String to int – Real Python
March 18, 2026 - In this example, str() is smart enough to interpret the binary literal and convert it to a decimal string. If you want a string to represent an integer in another number system, then you use a formatted string, such as an f-string (in Python 3.6+), and an option that specifies the base:
🌐
DataCamp
datacamp.com › home › tutorials › python › python data type conversion: a guide with examples
Python Data Type Conversion: A Guide With Examples | DataCamp
February 16, 2025 - Explicit data type conversion, often referred to as type casting, occurs when you deliberately convert a value from one data type to another. This approach gives you direct control over the conversion, as you explicitly specify the target data type in your code. It is commonly used when the automatic (implicit) conversion provided by Python doesn’t meet your requirements.
🌐
Vultr Docs
docs.vultr.com › python › built-in › int()
Python int() - Convert Value to Integer | Vultr Docs
November 22, 2024 - The int() function in Python is a standard function that converts a given input into an integer. This includes parsing integers from different types such as strings or floating-point numbers, especially useful in scenarios where data type conversion is essential, such as user inputs or processing external data files. In this article, you will learn how to ...
🌐
Reddit
reddit.com › r/learnpython › [beginner] how to best convert a 64bit integer to a 32 bit float
r/learnpython on Reddit: [beginner] How to best convert a 64bit integer to a 32 bit float
August 9, 2022 -

I was trying numpy for matrix calculations and I used it to solve simultaneous eqns.

I have a matrix, `answer` with desired values shown as:

[[4.]
[2.]
[5.]]

I realise that the dots are because the actual values are a float, so I tried `answer.tolist()`

This gives me:

[[3.9999999999999987], [1.9999999999999996], [4.999999999999998]]

In my program, I want to convert this to an integer, however using Python's `int()` function means it becomes: 3, 1, 4

I also tried using `.astype()` to convert to an int:

answer.astype(int,casting='same_kind'))

but I get:

TypeError: Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'same_kind'

I am sure importing the ceiling/floor function from `math` would solve this, but I am aware that some results may end up being normal decimals of maybe 4dp, rather than .999999 or .111111 recurring, so rounding isn't the best option

Any advice on the best way of converting?

Find elsewhere
🌐
Python.org
discuss.python.org › python help
Combine byte values to a 32bib int - Python Help - Discussions on Python.org
January 20, 2025 - I get 4 8bit (1 Byte) values from a i.e. serial input. In fact it is unix time in pieces, but to work with it I need it in one 32 bit int · Whether it’s a signed ('i' / int32 ) or unsigned('I' / uint32) integer was not specified, but try: · Source code: Lib/struct.py This module converts ...
🌐
GeeksforGeeks
geeksforgeeks.org › convert-string-to-integer-in-python
Convert String to Int in Python - GeeksforGeeks
Hexadecimal representation is commonly used in computer science and programming, especially when dealing with low-level operations or data encoding. In Python, converting a hex string to an integer is a frequent operation, and developers have multiple approaches at their disposal to achieve this tas
Published: October 27, 2024
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-data-types-in-python-3
How To Convert Data Types in Python 3 | DigitalOcean
August 20, 2021 - This Python 3 tutorial will guide you through converting data types including numbers, strings, tuples and lists, as well as provide examples to help familia…
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-convert-a-number-into-32-bit-binary-format
Python Program to Convert a Number into 32-Bit Binary Format - GeeksforGeeks
July 23, 2025 - In this article, we explored five different methods to convert a number into a 32-bit binary format using Python. Each method provides a unique way to achieve the desired result. Whether you prefer concise one-liners or a more explicit approach, these methods allow you to perform the conversion efficiently.
🌐
Career Karma
careerkarma.com › blog › python › python string to int() and int to string tutorial: type conversion in python
Python String to Int() and Int to String Tutorial: Type Conversion in Python
December 1, 2023 - Python includes built-in methods that can be used to perform these conversions: int() and str(). In this tutorial, we’ll explore how the int() method can be used to convert a string to an integer in Python.
🌐
Manifoldapp
cuny.manifoldapp.org › read › how-to-code-in-python-3 › section › 517db09e-e5a2-4b02-bd3d-d5406a3b40a8
How To Convert Data Types | How To Code in Python 3 | Manifold @CUNY
By using the float() function, we can convert integers to floats. Python also has a built-in function to convert floats to integers: int().
🌐
W3Schools
w3schools.com › python › ref_func_int.asp
Python int() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The int() function converts the specified value into an integer number. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com