Skip to content

chanakyavasantha/LearnPython

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PYTHON BASICS:


range function in python:

Screenshot from 2023-08-02 21-01-07

Screenshot from 2023-07-13 14-18-38# Learn Python

Questions on loops in python:

  • counting Number of Digits in a given number Screenshot from 2023-07-12 14-02-38 Screenshot from 2023-07-12 14-04-04

  • Sum of First n even numbers Screenshot from 2023-07-12 14-19-59 Screenshot from 2023-07-12 14-21-27

  • Algorithm for checking whether a given is a prime or not: Screenshot from 2023-07-13 14-10-27 Screenshot from 2023-07-13 14-11-19

Questions on loops in python:

  • Find Sum of first n natural numbers in python
  • Optimized Approach Screenshot from 2023-07-16 17-09-40
  • Better Alogorithms could solve problems in a optimized approach

Functions in python:

Screenshot from 2023-07-13 14-16-00 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

Examples:

  • What is the return type for input()?
    • String is the return type of input() function
    • Screenshot from 2023-07-13 14-21-22
    • Screenshot from 2023-07-13 14-26-42
  • Write a function in python that tells whether a given numbers is an even number:
  • Screenshot from 2023-07-16 17-38-43
  • Screenshot from 2023-07-16 17-38-06

Exercise on Functions:

  • 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)
  • Implement a function that reverses a given string. Screenshot from 2023-08-02 21-37-54
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 Screenshot from 2023-08-03 21-15-20
  • 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)

Screenshot from 2023-08-03 21-21-02

  • Write a function that removes duplicates from a list Screenshot from 2023-08-03 21-44-04

  • How Does jupyter notebook Work, How does it stores local variables data, how data is overriden?

Releases

No releases published

Packages

No packages published

Languages