#Basic Python Tutorial - Lesson One #How data types work num_1 = input("Give me a number: ") num_2 = input("Give me another number: ") print("At the moment, I am treating them as strings.") print(num_1,"+",num_2, "=" ,num_1 + num_2) print("But if I treat them as integers we get this.") print(num_1, "+", num_2, "=", int(num_1) + int(num_2)) #When we treat the numbers as integers it will combine their total value #Whereas when we treat the numbers as 'Strings' the script will place them side by side #in the order they have been inputed. #e.g. # num_1 = input("Give me a number: ") - For the purpose of this tutorial we have inputed the varible 2 # num_2 = input("Give me another number: ") - Same again here # # # print(num_1,"+",num_2, "=" ,num_1 + num_2) - When we treat them as strings the output will be , 22 # # print(num_1, "+", num_2, "=", int(num_1) + int(num_2)) - But when we treat them as integers we get, 4 # # # When it is treating the variables as strings, adding them together just prints them out one after another # (we call this “concatenating” strings). When I use the format “int(num_1)”, it tells the computer to convert the string # into an integer and then do the maths. If you give the computer numbers with decimals, you’ll see an error when it tries to add them. ######################## # == END OF TUTORIAL== # ########################
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question