Understand types of code?

can someone explain how to deal with different types of codes and when the code is string or int or…

Care to elaborate? what do you mean ‘deal with’ ? and what’s your purpose in “dealing with” the different types? Something like multiplying a string with an int?

Shure I care
i mean exactly what you said

Python language provides a built-in method to transfer string into int and float type or the other around. like this:

a_str = '12'
b_num = 3
result = int(a_str) * b_num

can you go deeper in explaining?
when should I use int() and when not?
when the code is a string and when it’s int …?
give me some examples, please
thank you so much

In simple terms, you can only operate two same types of variables,otherwise, you should get an error report, I’ll show you some examples:

a = 1  # a number 
b = "0.5" # a string ,but can be transfered to a int number
c = '10' # a string ,but can be transfered to a float number
d = "not numeric"  # pure string which cannot be transfered to a number

print(a + b)  # this will report error message for I plus a int number with a string

print (b + c) # this output will be "0.510" instead of 10.5 because a string plus a string means concat, not add

print(a + int(c) )  # this output will be 11 because I use 'int()' method to transfer the string into a integer

print( a+ float(b) )   # this output will be 1.5 because  I use 'float()' method to transfer the string into a float number

print( a + int(d) )  # this will report error message because you can never transfer the variable 'd' into a number

I get it
this is so useful and helpful
thank you so much for helping

Don’t mention it!I’m glad I can help.

1 Like