Python基础

1. eval() : the eval function evaluates the “String” like a python expression and returns the result as an integer

Syntax:  eval(expression, [globals[, locals]])

The arguments or parameters of eval function are strings, also optionally global and locals can be used as an argument inside eval function, but the globals must be represented as a dictionary and the locals as a mapped object.

Difference between the input() and eval(): input() takes the user input, but when the user enters an integer as an input the input function returns a string, but in the case of eval it will evaluate the returned value from a string to an integer. E.g:

input = input("Enter any number of your choice:")
print(input)
print(type(input))
--------------------------------------------------------------------Enter any number of your choice: 10 + 10
10 + 10
<class 'str'>


eval = eval(input("Enter any number of your choice"))
print(eval)
print(type(eval))
--------------------------------------------------------------------Enter any number of your choice: 10 + 10
20
<class 'int'>

 

 闷了请做题

2. String is immutable. If we want change sth, how?
--> Change to list, then update, change to string
--> or slice the string, then join back
3. String built-in string validation methods
isalnum(); isalpha(); isdigit(); islower()
posted @ 2020-03-30 22:40  Nora_L  阅读(101)  评论(0)    收藏  举报