Simple / Basic Python Programming examples for python begineer's

Reading Time:- 2 min 58 sec

Reading Time:- 2 min 58 sec


Avatar

 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.

Python Examples

Simple Python Examples


1. Hello, World!

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!"
                

 

2. Add Two Numbers

This example describes the Addition of two numbers in python.

num1 = 2 
num2 = 3.5
sum = num1 + num2
print(sum)



ADVERTISEMENT




3. Swap Two Variables

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)

4. Find the Square Root

Finding the Square Root Program using Python.

num = 5
num_sqrt = num ** 0.5
print(num_sqrt)

    


ADVERTISEMENT




5. Positive or Negative

Finding the number is Positive or Negative in Python.

num = 3
if num > 0:
    print("Positive")
elif num == 0:
    print("Zero")
else: 
    print("Negative")

    

6. Generate a Random Number

Generating a Random Number in Python Programming.

import random

# Between 0 to 9
print(random.randint(0, 9))
    


ADVERTISEMENT




7. Read from the Input

Reading the Input from the user in Python.

name = input("Your name: ")
print("Your name is", name)
    

8. Sum of Natural Numbers

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




9. Convert the Bases

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")

    

10. Even or Odd

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




11. Factorial of a Number

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)

12. Find ASCII Value

Finding the ASCII value of any Character, Number, or Symbol in Python Program.

char = "A"
print("ASCII value:", ord(char))



ADVERTISEMENT




13. Length of String

Finding the length of a String in Python Programming example.

s = "ProgramTuts.com"
print(len(s))

14. Type of Variables

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




15. Display Calendar

Displaying the Calendar in Python Programming.

import calendar

yy = 2021 # Year
mm = 3 # month - Mar

print(calendar.month(yy, mm))

16. Reverse a String

Finding the reverse of a string in Python.

s = input("Enter a String: ")

print(s[::-1])



ADVERTISEMENT




17. Check Prime Number

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")

18. Larger Number Function

Finding the Largest number using a Function in Python

def max(a, b):
    if a < b:
        return a
    else:
        return b

max(5, 2)

19. Make a Time Delay

Making a Time Delay using python inbuild function.

import time

print("Task A")
time.sleep(2) # 2 Seconds
print("Task B")



ADVERTISEMENT




20. Simple Calculator

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



Search
Advertisement
Advertisement
Share With
Follow us on
Advertisement
Advertisement