Mimi 2020年07月01日 カード13 いいね0



  • float
    Another example of type conversion is turning user input (which is a string) to numbers (integers or floats), to allow for the performance of calculations.
  • int
    In Python, it's impossible to complete certain operations due to the types involved. For instance, you can't add two strings containing the numbers 2 and 3 together to produce the integer 5, as the operation will be performed on strings, making the result '23'. The solution to this is type conversion.
  • integer
  • >>> float(input("Enter a number: ")) + float(input("Enter another number: "))
    Enter a number: 40 Enter another number: 2 42.0
  • >>> int("2") + int("3")
  • Variables
  • Variables
    Variables play a very important role in most programming languages, and Python is no exception. A variable allows you to store a value by assigning it to a name, which can be used to refer to the value later in the program. To assign a variable, use one equals sign. Unlike most lines of code we've looked at so far, it doesn't produce any output at the Python console
  • del
    Trying to reference a variable you haven't assigned to causes an error. You can use the del statement to remove a variable, which means the reference from the name to the value is deleted, and trying to use the variable causes an error. Deleted variables can be reassigned to later as normal.
  • >>> foo = "a string" >>> foo 'a string' >>> bar NameError: name 'bar' is not defined >>> del foo >>> foo
    NameError: name 'foo' is not defined
  • x += 3
    'x = x + 3'
  • str
    The str() function converts the specified value into a string.
  • print("My age is " + str(100))
    My age is 100
  • elif
    else:, if: