This question already has answers here:
What is print(f"...")
(7 answers)
Closed 2 years ago.
(Python Guess the Number)
where this f come from? or what does this f means?
Here is the code:
guess = int(input(f 'Guess a number between 1 and {x}: '))
Thats a representation called f-string, they started supporting them in Python 3.6
https://realpython.com/python-f-strings/
A string with an 'f' in the beginning is called an f-string. It is one of the new python 3.6 features. You can read more about it in these links:
Tutorial on Python 3's f-strings
Official Documentation from python.org
Related
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
String formatting in Python 3
(5 answers)
Closed 2 years ago.
I have python 3.7.4 and it has f string support. But the server I am porting my data has python < 3.6 and so does not support f string. The following code works fine in local machine:
directory_root = 'dataset_test/'
root_dir = listdir(directory_root)
for animal_folder in root_dir:
animal_folder_list = listdir(f"{directory_root}/{animal_folder}")
But this code fails in server as it does not support f string. How could I rewrite it using format ?
You can write it like this:
listdir("{directory_root}/{animal_folder}".format(directory_root=directory_root, animal_folder=animal_folder))
Or
listdir("{}/{}".format(directory_root, animal_folder))
You can simply use string formatting like following
"Your string {0} {1}...{}".format(variable1, variable2, variable)
The correct way, that works in every environment, not only on your local linux box would be:
animal_folder_list = listdir(os.path.join( directory_root, animal_folder))
When you explicitly specify folder separator (/), it will break on Win-boxes or in other hostile environments. os.path.join() comes to the rescue =)
This question already has an answer here:
Preincrement operators in python
(1 answer)
Closed 5 years ago.
I am a beginner in python and I am using Python 3.5. The python console complains invalid syntax for the below statement:
a = 5
print(a++)
But print(++a) works fine. Can anyone help me understand the difference?
Btw, it seems that print(a+=1) also doesn't work.
Thanks!
++a is just the same as doing (+(+a)). I.E: You're using the mathematical addition operator on the variable a (with implied zeroes). So the result is a
a++ is not valid python syntax (unlike other languages).
a += 1 is an assignment. It is equivalent to a = a + 1 - you can not print an assignment
This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I am trying to solve a problem using python. In which I have to deal with large integers (upto 500 digits). According to my current stage of understanding, python can handle any numbers in same traditional way. But I have problem in simple addition like this:
>>> p= 1001101111101011011100101100100110111011111011000100111100111110111101011011011100111001100011111010
>>> q= 0011111011111010111101111110101101111001111111100011111101101100100011010011111011111110110011111000
>>> p+q
1001101111105557844987142979708366943425581971579987152809865568761000527613931421735161949470823522L
Can anyone please explain why i got such an error.
Var q starts with a zero, making it an octal number, rather than decimal
This question already has answers here:
Add 'decimal-mark' thousands separators to a number
(9 answers)
Closed 8 years ago.
I am simply trying to execute the following command in Python:
print("Number is", format(49000.12345,"10.2f"))
So I would like it to print out like 49,000.12 for the number.
My teacher taught us to put a comma in "10.2f" like ",10.2f" for the thousands separator but it's not working. Could someone please tell me the correct simple way similar to that?
Thank you
See this: http://www.python.org/dev/peps/pep-0378/ It is the PEP introducing the ability into Python 2.7, 3.1 and later versions.
This question already has answers here:
What does `<>` mean in Python?
(5 answers)
Closed 9 years ago.
So, I'm making a python cheat sheet for myself, and when I started covering comparison operators, I noticed these two:
a = 1
b = 2
if a != b:
print("Dunno")
if a <> b:
print("Dunno")
I'm using python 2.7 and was curious if there's a difference between the two operators?
As described in the documentation, they are the same. <> is deprecated and was removed in Python 3, so you should use !=.
<> is deprecated. Other than that, no.