Python: how to display exact decimal value - python-2.7

I have a dictionary like
>> dic = {'yearly': 79.00, 'monthly': 59.00}
when the dic is printed the, it removes the last zero from the decimal number.
>> print dic
>> {'monthly': 59.0, 'yearly': 79.0}
How to get the original value as 59.00 not as 59.0 ?

when you print a number, you could do
x = 5
print '%.2f' % x
where the 2 specifies u want 2 decimal place
alternatively, the more updated/versatile version is
print '{:.2f}'.format(x)
if you really want your dict object to print nicely, you could create a custom class that specifies the __str__ function to print the dict however you want to print it

Related

How do I have a user enter values between 1 and 5, where 1 will return the employee information saved in the list index [0] in python [duplicate]

I am very new to programming in Python, this is something that seems so simple, but I just don't seem to be able to get it right.
I have a list of values.
I want to prompt the user for input.
Then print out the value that's at the corresponding index number in the list.
myList [0, 1, 20, 30, 40]
choice = input()
print (......)
If the user inputs 2, I want to print the the value that is at the index 2 (20). I am unsure of what to put after print.
You can do this by accessing the list using the given input by the user.
The input given by the user I assume would be a String, so we need to use int() to change it to an integer.
print myList[int(choice)]
Additionally, you may want to first check the validity of the input supplied; it cannot be less than 0, or more than the length of the list - 1;
choice = int(choice)
if (choice < 0 || choice >= len(myList))
print('Not Valid')
else
print myList[int(choice)]

How can i list a number, which this number is gave from user, in Python 2.7.14?

More accurately, I ask user for a number and after I want to list this number so i can use it for my code!
This is the part of my code i want help:
num=int(raw_input("Give me a number: "))
(*) (*)
#(*)...(*) is the part I want help!
try this :
numbers = []
num = int(raw_input('give me a number'))
numbers.append(num)
print numbers
or keep add items to the list :
numbers = []
while True:
num = int(raw_input('give me a number'))
numbers.append(num)
if the input is 10 20 30 40 50
you ca get the input as follows
number=map(int,raw_input("Enter the list of numbers:").split())
print number
this will have
number=[10,20,30,40,50]

Questions about Tuples

So I was able to run part of a program doing below (using tuples)
def reverse_string():
string_in = str(input("Enter a string:"))
length = -int(len(string_in))
y = 0
print("The reverse of your string is:")
while y != length:
print(string_in[y-1], end="")
y = y - 1
reverse_string()
The output is:
Enter a string:I Love Python
The reverse of your string is:
nohtyP evoL I
I am still thinking how for the program to reverse the position of the words instead of per letter.
The desired output will be "Phython Love I"
Is there anyway that I will input a string and then convert it to a tuple similar below:
So If I enter I love Phyton, a certain code will do as variable = ("I" ,"Love", "Python") and put additional codes from there...
Newbie Programmer,
Mac

Find the the total number of 1's in binary form for a group number's in a list in python 3

I want to count total number of '1's in binary format of a number which is in a list.
z = ['0b111000','0b1000011'] # z is a list
d = z.count('1')
print(d)
The output is 0.
Whereas the required output should be in the form of [3,3]
which is number of ones in every element that Z is containing :
Here it is :
z=['0b111000','0b1000011']
finalData = []
for word in z:
finalData.append(word.count('1'))
print(finalData)
The problem with your code was you were trying to use count() method on list type and it is used for string. You first need to get the string from the list and then use count() method on it.
Hope this helps :)
z = ['0b111000','0b1000011']
d = z.count('1')
This attempts to find the number of times the string '1' is in z. This obviously returns 0 since z contains '0b111000' and '0b1000011'.
You should iterate over every string in z and count the numbers of '1' in every string:
z = ['0b111000','0b1000011']
output = [string.count('1') for string in z]
print(output)
# [3, 3]
list.count(x) will count the number of occurrences such that it only counts the element if it is equal to x.
Use list comprehension to loop through each string and then count the number of 1s. Such as:
z = ['0b111000','0b1000011']
d = [x.count("1") for x in z]
print(d)
This will output:
[3, 3]

Python remove odd numbers and print only even

user = int(raw_input("Type 5 numbers"))
even = []
def purify(odd):
for n in odd:
even.append(n)
if n % 2 > 0:
print n
print purify(user)
Hello I am a beginner and I would like to understand what is wrong with this code.
The User chose 5 numers and I want to print the even numbers only.
Thanks for helping
There are a few problems:
You can't apply int to an overall string, just to one integer at a time.
So if your numbers are space-separated, then you should split them into a list of strings. You can either convert them immediately after input, or wait and do it within your purify function.
Also, your purify function appends every value to the list even without testing it first.
Also, your test is backwards -- you are printing only odd numbers, not even.
Finally, you should return the value of even if you want to print it outside the function, instead of printing them as you loop.
I think this edited version should work.
user_raw = raw_input("Type some space-separated numbers")
user = user_raw.split() # defaults to white space
def purify(odd):
even = []
for n in odd:
if int(n) % 2 == 0:
even.append(n)
return even
print purify(user)
raw_input returns a string and this cannot be converted to type int.
You can use this:
user = raw_input("Input 5 numbers separated by commas: ").split(",")
user = [int(i) for i in user]
def purify(x):
new_lst = []
for i in x:
if i % 2 == 0:
new_lst.append(i)
return new_lst
for search even
filter would be the simplest way to "filter" even numbers:
output = filter(lambda x:~x&1, input)
def purify(list_number):
s=[]
for number in list_number:
if number%2==0:
s+=[number]
return s