Python
1: Print Function
2: Escape Sequence
3: Comments
4: Escape Sequence as Normal Text
5: Print Emoji
6: Python as Calculator
7: Strings Concatenation
8: Input
9: int function
10: Variables
11: String Formatting
12: String Indexing
13: String Slicing
14: Step Argument
15: Some useful function and methods
16: Strip Method
17: Find and Replace method
2: Escape Sequence
3: Comments
4: Escape Sequence as Normal Text
5: Print Emoji
6: Python as Calculator
7: Strings Concatenation
8: Input
9: int function
10: Variables
11: String Formatting
12: String Indexing
13: String Slicing
14: Step Argument
15: Some useful function and methods
16: Strip Method
17: Find and Replace method
Chapter 9: int function
If we need to perform mathematical calculations with user input, we must convert the input to an integer using the int() function; otherwise, it will default to a string. While two strings can be concatenated, mathematical operations require the strings to be converted to integers first. To simplify this, we use the int() function for conversion.
num1 = input("Enter number one: ") # prompt the user to enter a value
num2 = input("Enter number two: ") # prompt the user to enter a value
num1 = int(num1)
num2 = int(num2)
sum = num1 + num2
print("The sum of these two numbers is: " + str(sum))
Output:
Enter number one: 5
Enter number two: 7
The sum of these two numbers is: 12