Basic Code for Python: Python is one of the most sought-after programming languages among developers. The reason is obvious, as this programming language is easy to learn and implement. Due to the large number of Python libraries and frameworks, development is easy. The best way to learn Python coding is by practising and solving problems based on Python. Read this article to learn some basic code for Python.
Highlights:
- Swapping Two Numbers: A Python program demonstrating how to swap two numbers using temporary variables is provided, showcasing user input, variable manipulation, and output display. The code exemplifies a fundamental programming concept frequently encountered in Python.
- Adding Two Numbers: A basic Python program to add two numbers is presented, illustrating user input, arithmetic operations, and output display. This simple code snippet serves as an introductory exercise for beginners to practice basic arithmetic operations in Python.
- Factorial of a Number: The content offers a Python function to calculate the factorial of a number using recursion, demonstrating function definition, conditional statements, and recursive function calls. This example introduces learners to algorithmic concepts and problem-solving strategies.
- Sum of First n Natural Numbers: A Python program to determine the sum of the first n natural numbers is provided, employing a loop structure for iterative addition. This code snippet emphasizes iterative problem-solving approaches and loop constructs in Python programming.
- Palindrome Check: A Python function to check whether a given string is a palindrome is presented, illustrating string manipulation, conditional statements, and function calls. This example introduces learners to string operations and algorithmic techniques for problem-solving.
Top 10 Basic Code For Python With Solution
Check out some of the basic Python codes with the solutions below.
1. Write a Python program to swap two numbers.
Ans: Check the Python code below to swap two numbers using Python.
Basic Code for Python |
num1 = float(input(“Enter the first number: “)) //prompt user for entering number
num2= float(input(“Enter the second number: “)) #Display the original number first print(f “Original Values: num1 = {num1}, num2 = {num2}”) #Swap two numbers using the temp variable temp = num1 num1 = num2 num2 = temp #Display the swapped numbers print(f “Value after Swapping values: num1 = {num1}, num2 = {num2}”) |
Output
Enter the first number: 14
Enter the second number: 51 Original values: num1 = 14, num2 = 51 Value after Swapping: num1 = 51, num2 = 14 |
2. Write a program to add two numbers using Python.
Ans: Adding two numbers in Python is one of the most basic Python problems for beginners. Check the input and output samples for the Python problem given above.
Basic Code for Python |
#Basic code for Python to add two numbers
num1 = float(input(“Enter the first number: “)) //prompt user for entering number num2= float(input(“Enter the second number: “)) #Create a variable to store the sum of the two numbers. sum = num1 + num2 print(f”The sum of {num1} and {num2} is {sum}”) |
Output
Enter the first number: 15
Enter the second number: 7 The sum of 15 and 7 is 22 |
3. Write a basic Python program for finding the factorial of a number.
Ans: Factorial of number is the repeated multiplication of first n integers. It can only be calculated for positive integers. Let us create a Python function to find the factorial of a number n.
Basic Code for Python |
#Function for finding the factorial of a number using Python
def factorial (n): if (n == 1 or n == 0): //If numbers are 0 and 1, then return 1. return 1 else: return n * factorial (n-1) //Factorial using recursion int num = int(input(“Enter a number to calculate factorial of number: ”) print(“Factorial of”, num, “is”, factorial(num)) |
Output
Enter the number to calculate the factorial:
6 Factorial of 6 is 720 |
4. Write a program to determine the sum of the first n natural numbers, Write a Python program.
Ans: The sum of the first natural number can be calculated directly using the formula n * (n+1) // 2. However, we will use the basic approach in the code below for beginners.
Basic Code for Python |
#Function to calculate the sum of n numbers using Python. It can be calculated using simple repeated addition.
num = int (input(“Enter the number: ”)) if num<0: print(“Negative numbers are not allowed.”) else: sum = 0 i = num while(i>0): sum+=i i = i-1 print(f “ Sum of first {num} natural number is”, num) |
Output
Enter the number: 6
Sum of first 6 natural number is 21 |
5. Write a Python program to check whether a string is a palindrome.
Ans: A palindrome is a type of string in which, on reversing, the digits are the same. For example, when we reverse “radar”, it is the same. These strings are palindrome.
Basic Code for Python |
#Python function to check whether a string is a palindrome.
def isPalindrome(str) #Remove spaces and convert them into lowercase letters. final_string = ‘ ’.join(str.split()).lower() return rev = final_string[: : -1] int str_input = input(“Enter a string: ” ) if (is_palindrome(str_input): print(f “{str_input} is a palindrome.”) else: print(f”{str_input} is not a palindrome.”) |
Output
Enter a string:
Radar Radar is a palindrome. |
6. Write a Python program to reverse a string.
Ans: In Python, strings can easily be reversed using slicing or a while loop in reverse order. Check the code below for implementation using Python.
Basic Code for Python |
#Python function to check whether a string is a palindrome.
str = input(“Enter a string: ”) reverse_str = str[ : : -1] print(f “The reversed string is {reverse_str}.”) |
Output
Enter a string:
Physics Wallah The reversed string is hallawscisyhP. |
7. Write a Python program to check whether a year is a Leap Year.
Ans: A Leap Year occurs every year, having 366 days. The one extra day is in February. A year that is divisible by 4 and not 100. Also, if they are divisible by 400, then it is a leap year.
Basic Code for Python |
#Function to predict whether a given year is a Leap year.
def is_Leap_Year (year): #If the given year is divisible by 4 and not divisible by 100, then it is a leap year. Also, if the given year is divisible by 400, then it is a leap year. if (year%4 == 0 and year%100!=0) or (year%400 ==0): return True else: return False year_inp = int(input(“Enter a year:”)) //Take input from the user. if is_Leap_Year(year_inp): print(f “{year_inp} is a leap year.”) else: print(f”{year_inp} is not a leap year.”) |
Output
8. Write a Python program to calculate the square of a given number.
Ans: The given number is multiplied twice to get the square of the given number. Check out the program below to calculate the square of a given number.
Basic Code for Python |
#First, take the input from the user
num = int(input(“Enter the number to find square: ”)) print (“Square of the given number is “,num*num) |
Output
Enter the number to find square:
9 Square of the given number is 81 |
9. Write a Python Program to check whether the given character is a vowel or consonant.
Ans: We can use if and else command statements to check whether a given character is a vowel or consonant. There are five vowels in the English alphabet: a, e, i, o, u. Check out the Python code below.
Basic Code for Python |
#Ask the user for input of a character (vowel or consonant)
ch = input(“Enter a character: “) if(ch == ‘a’ or ch == ‘e’ or ch == ‘i’ or ch == ‘o’ or ch == ‘u’): print(“The given character”, ch, “is a vowel.”) //if the character is found to be vowel else: Print (“Given character”, ch, “is a consonant.”) //if the given character is consonant. |
Output
Enter a character:
e The given character e is a vowel. |
10. Write a Python program to remove spaces from a string without using inbuilt functions.
Ans: We have to write a Python program without using any built-in function to remove spaces between a string. Check the implementation below.
Basic Code for Python |
#First take input from the user
str = input(“Enter a string: ”) result = “ ” #iterate string one by one for i in str: if i!= ‘ ’: result +=i print(“The given string after removing all spaces is: ”, result) |
Output
Enter a string
Physics Wallah The given string after removing all spaces is: PhysicsWallah |
Learn Python with PW Skills
Enrol in our Decode Python with DSA Course to learn Python programming language and start a career as a Python developer. The course is taught by industry-level experts from top MNCs and with a large network of collaborators we also help our students with 100% placement assistance. Hurry! and grab some exciting offers only at @pwskills.com
Basic Code for Python FAQs
How do I write basic code in Python?
First, create a file using the .py extension in Python. Now open the file in the text editor and start with the Hello World program.
What is the easiest Python code?
Some of the easiest Python codes are mentioned below:
Printing Hello World in Python.
Finding the addition of two numbers
Swapping two numbers
Finding the square or cube of a number
There are many more easy Python codes that you can find in this article.
Is Python a very difficult language?
Python is an easy language that is preferred by most programs worldwide. It consists of many libraries and frameworks to make development easy and effective.
What is better, Python or C++
Python is used for various purposes because it can easily integrate with the latest technologies and applications. The ease of use and flexibility of Python make it more favourable for programmers. However, choosing a language has no constraints. You can totally rely on your choice to find the best language for yourself.