Getting the number in the ones place python3 [duplicate] - python-3.x

This question already has answers here:
How to check last digit of number
(10 answers)
Closed 4 years ago.
I was wondering if there was any way to get the ones place of a integer. For example, if I had the number 41, would there be any easy way to get 1 from that? Thanks!

Take modulo by 10
a = float(input('Enter a number : '))
print( a % 10)
Modulo operator return the remainder of number so any number modulo with 10 will return its ones number for more https://docs.python.org/3/reference/expressions.html

Related

How to extract number from string in pandas? [duplicate]

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
RegEx - Match Numbers of Variable Length
(4 answers)
Closed 4 months ago.
My case is extract number between text (ex: FL-number-$) from string in File names column to Check column. Example:
2022-06-09-FR-Echecks.pdf > Return ''
2022-06-09-FR-FL-3-$797.pdf > Return 3
2022-06-09-FR-TX-20-$35149.91.pdf > Return 20
My case as below
This code I used:
dt_test['File_names_page'] = dt_test['File names'].str.extract('\-([0-99])-\$')
It only return one digit number as below:
So how to extract all number (all digit) in my case?
Tks for all attention!
Your regex pattern is slightly off. Just use \d+ to match any integer number:
dt_test["File_names_page"] = dt_test["File names"].str.extract(r'-(\d+)-\$')
You can't use a 0-99 range, you should use \d{1,2} for one or two digits:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d{1,2})-\$')
Or for any number of digits (at least 1) \d+:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d+)-\$')
NB. - doesn't require an escape
Example:
File names File_names_page
0 ABC-12-$456 12

Float with no more than 3 decimals [duplicate]

This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 10 months ago.
How to print the calculation of float with only 3 more decimal
If I calculated something and the answer of that calculation is like, 3.33333333...
I just want to print it like 3.333 and that's it
With the round function:
round(3.33333333, 3)
You can use format specifiar
num = 3.3333333
print("%.3f"%num)
Output:3.333
a=1.233455543323
print('{0:.3f}'.format(a))
output=1.233
you can use round function
syntax:
round(number, digits)
number: Required. The number to be rounded
digits: Optional. The number of decimals to use when rounding the number. Default is 0
num = 3.33333333
print(round(num, 3))

Operand for matching any one of multiple cases [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
I would like to have an if statement where the match is one of several numbers without using a large number of | bars because I have many matching cases
if number == 1 | number == 5 | number == 7
do stuff
In R, there is the operand %in% that works sort of like the following:
if number %in% [1,5,7]
do stuff
Is there a similar operand/ability in python?
Thanks
You can use something like
if number in [1, 5, 7]:

Python 3 : print float precision without zero followed [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 5 years ago.
For example
x = 1.23
print("%.3f" % x)
Output will be "1.230" and I want "1.23".
Is there any way to do? Thank you.
edited:
I want a function that will print floating point at limit precision and will not print zero followed if it's not reach the limit given
If you want to output up-to 3 digits but without 0 you need to format to 3 digit and rstrip zeros:
for n in [1.23,1.234,1.1,1.7846]:
print('{:.3f}'.format(n).rstrip("0")) # add .rstrip(".") to remove the . for 3.00003
Output:
1.23
1.234
1.1
1.785
Have a quick read here for python 3 formattings: https://pyformat.info/#number or here: https://docs.python.org/3.1/library/string.html#format-examples

python3: how do i format 2 as 02 [duplicate]

This question already has answers here:
Display number with leading zeros [duplicate]
(19 answers)
Closed 8 years ago.
Apologies if this is a repeat question but,
what formatting commands do i need to use if I want a single digit number to be displayed with a zero in front?
i.e. the number '2' would be displayed as '02'.
[But, I do not want any value above 10 to have extra zeros in front]
cheers
You can use this syntax:
>>> "{:0>2}".format(2)
'02'
>>> "{:0>2}".format(98)
'98'
>>> "{:x>4}".format(2)
'xxx2'
More info: Common string operations
Try:
if 0 < num < 10:
return "0" + str(single_digit)

Resources