Can you help me return Python age-specific information to a string, to a string, to an integer?

Asked 2 years ago, Updated 2 years ago, 29 views

1. def generation(x):
We need to make a function that returns a string to "10", "20", and "30" by taking the numerical age value.

def generation(x):
    return(int(age)+"large")

This is the problem, but will this generation function that has not yet received the age value be like this?
age=int(input())
I used int because I think I should get my age as an integer.

2. sum(a,b):
If a, b is a string, you must return the string plus value, if a and b are integers

I've tried a lot of different ways, but if I write a conditional sentence, I can't put the code in one line.
return "x"+"y" If this is the case, str appears that the string cannot be added If you write an integer int() after that, the type is also a problem here, but how can you do this in one line?

Something's wrong with the approach.

python

2022-09-21 14:35

1 Answers

Python is a dynamic type determination language.

That is, you don't know what type it is until you run it in the language where the type is determined at the time of execution. This sometimes acts as a big drawback for Python.

In [73]: a = 'aaa'                                                              

In [74]: b = 'bbb'                                                              

In [75]: a + b                                                                  
Out[75]: 'aaabbb'

In [76]: n1 = 10                                                                

In [77]: n2 = 20                                                                

In [78]: n1 + n2                                                                
Out[78]: 30

In [84]: a                                                                      
Out[84]: 'aaa'

In [85]: a.__add__('nnnnn')                                                     
Out[85]: 'aaannnnn'

In [86]: n1                                                                     
Out[86]: 10

In [87]: n1.__add__(30)                                                         
Out[87]: 40

Python calls _add__ when calling +.

Python is actually a pretty difficult language. There is no data type, and the results come out on the interactive screen, so it just looks like an easy-to-handle language, but there are many things to know. The representative of them is the magic method.

Python can overload operators, so you must be aware of the magic methods. Please refer to Python's official help for this part.

In other words, as you can see above, both strings and integers implement _add__, so just add +.

In [80]: def sum2(var1, var2): 
    ...:     ...:     return var1 + var2

In [81]: sum2(1, 2)                                                             
Out[81]: 3

In [82]: sum2('aaa', 'bbb')                                                     
Out[82]: 'aaabbb'

And the first question is like homework, right?

The above requirements are simple and can be implemented simply as a single-line lambda.

It's just one line. Please analyze it.

In [67]: generation = lambda x:'{}Large.format(int(x/100 * 10) * 10) if x > 9 else 'less than 10'            

In [68]: generation(17)                                                         
Out [68]: 'Teenager'

In [69]: generation(27)                                                         
Out [69]: '20s'

In [70]: generation(7)                                                          
Out [70]: 'Under teens'


2022-09-21 14:35

If you have any answers or tips


© 2025 OneMinuteCode. All rights reserved.