Exemplification is the greatest method to learn Python. This page offers Python fundamental concept examples. Before looking at the solution, we advise you to try these examples on your own. All of the applications on this page have been tested and should run on any platform.
Hello, World! Python Program!
# This program prints Hello, world! print('Hello, world!')
We used the built-in print() function in this programme to produce the string Hello, world! on our screen. A string, by the way, is a character sequence. Strings in Python are surrounded by single, double, or triple quotations.
Python Program to Add Two Numbers
# This program adds two numbers x = 2.5 y = 9.2 # Add two numbers sum = x + y # Display the sum print('The sum of {0} and {1} is {2}'.format(x, y, sum))
# Store input numbers x = input('Enter first number: ') y = input('Enter second number: ') # Add two numbers sum = float(x) + float(y) # Display the sum print('The sum of {0} and {1} is {2}'.format(x, y, sum))
We asked the user to enter two numbers in this programme, and it then displays the sum of the user-inputted two integers. To take the input, we employ the built-in function input(). Since input() returns a string, we use the float() method to change the string into a number. The numbers are then added.
Your email address will not be published. Required fields are marked *
Save my name, email, and website in this browser for the next time I comment.