Variable with " " in Python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Trying to make a for loop work in Python I came across a variable declared with "".
i.e: res=""
What is the purpose of the quotes?
Want to know what happens in there.

res="" is an empty string. This can be used later to, for example:
Add another string to it: res += "ABC"
Check whether there is a result. An empty string returns False, while a string with at least 1 character returns True.
Check the length of the string by len(res).
I could go on but you get the point. It's a placeholder.

Related

How do I use .any() to print the value I am looking for? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
If I have the following code
if "ERB-776-RAD" in my_data.any():
print("I found it!")
Where the my_data is something as follows:
my_data = ['ERA-776-TRF','FDS-342-FHS','EBR-776-RAD'...]
How would I print the actual value I am looking for (ERB-776-RAD) instead of printing "I found it"?
Use the in statement to test if the list contains the string. Then you can print anything you want if it's True. You can use string formatting to insert the value dynamically (in the code below, I am using f-strings).
my_data = ['ERA-776-TRF','FDS-342-FHS','EBR-776-RAD']
target = "ERB-776-RAD"
if target in my_data:
print(f"I found {target}")
As #chepner commented, lists do not have an any method. (If you saw .any(), it might have been with a numpy array or pandas dataframe.)

The commands I type after the if command in python do not work [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I wrote what should show if 2 is called in the if command in Python, but it doesn't work.
if baslangic == 2:
print("meslekler:",meslekler)
When you're using input to read in the value of baslangic, it will be a string by default.
Comparing a string to a an integer will never be True ('2' vs 2 - one is a numeric value, the other is a sequence of characters), so you'll have to convert them to the same format.
Either use int(input('Foo? ')) to convert the input to an int when you read it (if the user is only expected to type in an integer), or compare it to a string:
if baslangic == '2':

How do to get python to not calculate number variable with dash? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to pass a variable '3-123' to a method in python, but if I do str(3-123) I get '-120'. Tried iterating, but I got an error cause it's an int.
You simply pass the string "3-123".
Your expression str(3-123) tells Python to first evaluate what is in parentheses, which is very clearly the arithmetic expression 3-123. That evaluation mandates a subtraction.
UPDATE PER USER COMMENT
Since you just got it returned from REST, then it's already a string. It seems that your problem is that you're building an expression string to be evaluated in SQL. In this case, you need to build the string you're going to send to SQL, at the character level. For this one item you would extend your 3-123 string with quotation marks:
from_rest = "3-123" # In practice, this comes directly from your REST return value.
to_sql = '"' + from_rest + '"'
This leaves you with a variable that contains the string "3-123" -- seven characters, rather than the original five.
Is that what you needed?

i want to write a python program that converts an integer to its corresponding month [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to add a error message if it exceeds 12 or if a character is entered without any if statements. here is what I have written so far:
import calender
def get_mont_name(month_number):
try:
return calender.month_name[month_number]
except IndexError:
print("'[]' is not a valid month number".format(month_number))
Your code is really close to working!
Firstly, you've spelt calendar wrong, but I assume this is just a typo.
You've slightly misused format() which is probably causing your issue. Replacing your print statement with the following should fix it:
print("{num} is not a valid month number".format(num = month_number))
The curly brackets allow the format() method to identify the parts of the string you'd like to modify.
I hope this helps!

How to split with multiple bracket [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I want to do something like this
"(1.0)" which returns ["1","0"]
similarly "((1.0).1)" which returns ["(1.0)", "1")
How do i do this python? Thanks for the help
so basically I want to break the string "(1.0)" into a list [1,0] where the dot is the separator.
some examples
((1.0).(2.0)) -> [(1.0), (2.0)]
(((1.0).(2.0)).1) -> [((1.0).(2.0)), 1]
I hope this is more clear.
Here is my version:
def countPar(s):
s=s[1:-1]
openPar=0
for (i,c) in enumerate(s):
if c=="(":
openPar+=1
elif c==")":
openPar-=1
if openPar==0:
break
return [s[0:i+1],s[i+2:]]
You'll need to build a little parser. Iterate through the characters of the string, keeping track of the current nesting level of parentheses. Then you can detect the . you care about by checking that first, the character is a ., and second, there's only one level of parentheses open at that point. Then just place the characters in one buffer or another depending on whether you've reached that . or not.

Categories