## Fix Python – From list of integers, get number closest to a given value

Given a list of integers, I want to find which number is the closest to a number I give in input:
>>> myList = [4, 1, 88, 44, 3]
>>> myNumber = 5
>>> takeClosest(myList, myNumber)

4

Is there any quick way to do this?
….

## Fix Python – Converting integer to binary in python

In order to convert an integer to a binary, I have used this code :
>>> bin(6)
‘0b110’

and when to erase the ‘0b’, I use this :
>>> bin(6)[2:]
‘110’

What can I do if I want to show 6 as 00000110 instead of 110?
….

## Fix Python – Why does integer division yield a float instead of another integer?

Consider this division in Python:
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
>>> 2/2
1.0

Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always ….

## Fix Python – Safest way to convert float to integer in python?

Python’s math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example:
import math
f=math.floor(2.3)

Now f returns:
2.0

What is the safest way to get an integer out of this flo….

## Fix Python – How to find length of digits in an integer?

In Python, how do you find the number of digits in an integer?
….

## Fix Python – Convert floats to ints in Pandas?

I’ve been working with data imported from a CSV. Pandas changed some columns to float, so now the numbers in these columns get displayed as floating points! However, I need them to be displayed as integers or without comma. Is there a way to convert them to integers or not display the comma?
….

## Fix Python – Python strings and integer concatenation [duplicate]

This question already has an answer here:

How can I concatenate str and int objects?

Closed 5 years ago.

I want to create a string using an integer appended to it, in a for loop. Like this:
for i in range(1, 11):….

## Fix Python – How do you express binary literals in Python?

How do you express an integer as a binary number with Python literals?
I was easily able to find the answer for hex:
>>> 0x12AF
4783
>>> 0x100
256

and octal:
>>> 01267
695
>>> 0100
64

How do you use literals to express binary in Python?

Python 2.5 and earlier: can express binary using int(‘01010101111’,2) but not with a lite….

## Fix Python – How can I convert a character to a integer in Python, and viceversa?

I want to get, given a character, its ASCII value.
For example, for the character a, I want to get 97, and vice versa.
….

## Fix Python – How do you round UP a number?

How does one round a number UP in Python?
I tried round(number) but it rounds the number down. Example:
round(2.3) = 2.0

and not 3, as I would like.
The I tried int(number + .5) but it round the number down again! Example:
int(2.3 + .5) = 2

….