**range function in python**: ![Screenshot from 2023-08-02 21-01-07](https://github.com/chanakyavasantha/LearnPython/assets/93817654/157b3a8d-3088-43aa-b578-c6722eeba6e6)
- Find Sum of first n natural numbers in python
- Optimized Approach
- Better Alogorithms could solve problems in a optimized approach
Function should be defined so that:
- it accepts input
- it gives some ouput
- The input have some specific data type
- The produced will also have some specific data type ![Uploading Screenshot from 2023-07-13 14-18-38.png
- Similiarly we have input data type and out put data type for a function in python
- The output data type is called return type
- What is the return type for input()?
- Write a function in python that tells whether a given numbers is an even number:
- Write a fucntion to check , whether a given number is prime.
- Use both Brute Force Approach, and Optimized Approach
## Python Program to check whether a given number is prime using Brute Force Approach
n = int(input()) # taking input from user
flag = 0
for i in range(2,n):
if n % i == 0:
flag = 1
break
if flag == 1:
print("The given number is not prime")
else:
print("The gven number is prime")
## Python Program to check whether a given number is prime using Optimized Approach
import math
n = int(input()) # taking input from user
flag = 0
for i in range(2,int(math.sqrt(n))+1):
if n % i == 0:
flag = 1
break
if flag == 1:
print("The given number is not prime")
else:
print("The gven number is prime")
- Write a function that takes two integers as parameters and returns their sum.
## Python Program to design a function that returns sum
def SUM(a,b):
return a+b
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = SUM(a,b)
print(c)
- Write a function that takes a list of numbers as input and returns the largest number in the list.
## Python Program to design a function that finds maximum in a list
def FindMax(li):
Max = li[0]
for i in range(len(li)):
if li[i] > Max:
Max = li[i]
return Max
li = list(map(int,input().split()))
c = FindMax(li)
print(c)
- ONE LINE CODE
li = list(map(int,input().split()))
c = max(li)
print(c)
def rev(a):
b = ""
for i in range(len(a)-1,-1,-1):
b += a[i]
return b # b will have reversed string
a = "chanakya"
print(rev(a))
a = "kushik"
b = a[::-1]
print(b)
- Implement a function that takes a list of words and returns a new list with the words sorted in alphabetical order.
def SortedList(li):
li.sort()
return li
li = input().split()
Li = SortedList(li)
print(Li)
split Function in python
li = list(map(int,input().split())) How does this work?
- Some Examples on split() function in python
- input() returns a string by default
- split() returns a list, it's argumnent is the seperator , default seperator is space
- input().split() returns a list of string seperated at space
- map type casts a list with given data type. and returns a map object.
- map object could be converted into list by list(map(....
- list(map(int,input().split())) returns a list of integers by mapping strings to their corresponding integer values.
li = list(map(int,['2','3','4']))
print(li)