Reading Time:- 2 min 58 sec
Reading Time:- 2 min 58 sec
BY TEJAS
For Python beginners, this article was really helpful. This article gives you a full idea of "Learning Python" with these starting examples. These set of examples help you to learn python programming quickly & easily. These examples are dependent on the basics of python. This article provides a set of some simple python code examples which helps you to try the basics of python.
So let's get started.
Simple Python Examples
Hello, World in python is a very common example in python. In this, we are printing the "Hello World!" in the console.
print "Hello, World!"
This example describes the Addition of two numbers in python.
num1 = 2
num2 = 3.5
sum = num1 + num2
print(sum)
ADVERTISEMENT
This python programming example swaps the values of two variables.
a = 2
b = 4
a, b = b, a, b
print("a =", a)
print("b =", b)
Finding the Square Root Program using Python.
num = 5
num_sqrt = num ** 0.5
print(num_sqrt)
ADVERTISEMENT
Finding the number is Positive or Negative in Python.
num = 3
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
Generating a Random Number in Python Programming.
import random
# Between 0 to 9
print(random.randint(0, 9))
ADVERTISEMENT
Reading the Input from the user in Python.
name = input("Your name: ")
print("Your name is", name)
Sum of the Natural Numbers in Python Programming.
num = 7
if num < 0:
print("Negative")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("Sum: ", sum)
ADVERTISEMENT
Converting the Decimal numbers into the Binary, Octal, & HexaDecimal number system.
dec = 5
print(bin(dec), "in Binary")
print(oct(dec), "in Octal")
print(hex(dec), "in HexaDecimal")
Finding the given number is Even or Odd using Python Programming.
num = 5
if(num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
ADVERTISEMENT
Finding the Factorial of a Number in Python Program.
num = 5
factorial = 1
if num < 0:
print("Does not exist")
elif num == 0:
print(factorial)
else:
for i in range(1, num + 1):
factorial = factorial * i
print(factorial)
Finding the ASCII value of any Character, Number, or Symbol in Python Program.
char = "A"
print("ASCII value:", ord(char))
ADVERTISEMENT
Finding the length of a String in Python Programming example.
s = "ProgramTuts.com"
print(len(s))
Finding which type of the value is stored in variable. E.g Str, Int, Float
s = "John"
i = 25
f = 1.5
print(type(s)) # str
print(type(i)) # int
print(type(f)) # float
ADVERTISEMENT
Displaying the Calendar in Python Programming.
import calendar
yy = 2021 # Year
mm = 3 # month - Mar
print(calendar.month(yy, mm))
Finding the reverse of a string in Python.
s = input("Enter a String: ")
print(s[::-1])
ADVERTISEMENT
Checking the Number is Prime or Not in Python.
num = 7
if num > 1:
for i in range(2, num):
if(num % i) == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
Finding the Largest number using a Function in Python
def max(a, b):
if a < b:
return a
else:
return b
max(5, 2)
Making a Time Delay using python inbuild function.
import time
print("Task A")
time.sleep(2) # 2 Seconds
print("Task B")
ADVERTISEMENT
Making a Simple Calculator which do basic Arithmetic Operations in Python Programming.
print("Enter an Operator (+, -, *, /)")
choice = input("Your Choice")
if choice in ('+', '-', '*', '/'):
num1 = float(input("1st number: "))
num2 = float(input("2nd number: "))
if choice == '+':
print(num1 + num2)
elif choice == '-':
print(num1 - num2)
elif choice == '*':
print(num1 * num2)
elif choice == '/':
print(num1 / num2)
else:
print("Invalid Input")
Give your feedback by commenting below on how you felt about this article.
ADVERTISEMENT
COMMENTS