Posts

Showing posts from August, 2020

28.Strings in Python

Image
Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes. Example: >>>s1="Python Programming" >>>s2='Python Programs' >>>s3="""Python is a powerful Programming language""" This will create a string object s1, s2 and s3 using different forms.Individual characters can be accessed using different subscripts( +ve or –ve) with the string object. The positions of string's characters are numbered from 0.So the 5th character is in position 4. When working with strings, the programmer sometimes must be aware of a string's length.Python len() function can be used to find the number of characters in the string Eg: >>> len('python') 6 >>> len("") 0 Note: The string is an immutable data structure.This means that its inter

27.Python Anonymous/Lambda Function

In Python, an anonymous function is a function that is defined without a name.These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions. The Anonymous Functions Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions. An anonymous function cannot be a direct call to print because lambda requires an expression. Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace. Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline statements in C or C++, whose purpose is by passing function stack allocation during invocation for performance reasons. Syntax of Lambda Function in python lambda [arg1 [,arg2,.....argn]]:expression La

26.The Software Development Process, case study

Image
There is much more to programming than writing lines of code.Computer Engineers refer to the process of planning and organizing a program as Software Development. There are several approaches to software development.One version is known as the waterfall model. The waterfall model consists of several phases: 1.Customer request: In this phase, the programmer receives a broad statement a problem to be solved. This is the user requirement specification phase. 2.Analysis: Determines what the program will do. This is viewed as the process of clarifying the specification for the problem. 3.Design: Determines how the program will do the task 4.Implementation: The programmers write the program. This step is also called coding phase. 5.Integration: Large programs have many parts.In the integration phase, these parts are brought together into a smoothly functioning system as a whole. 6. Maintenance: Programs usually have a long life, a span of 5-15 years is common.During this time, requirements

25.Programming Assignment 6( using recursion)

1.Print the factorial of a number. 2.Find nCr using a recursive factorial function. 3.Find the sum of the digits of a number. 4.Generate n’th Fibonacci number.  5.Generate the Fibonacci series using the recursive function written in qno-4. 6.Convert a decimal number into octal using recursion. 7.Convert the decimal number into binary using recursion. 8.Sum of n natural numbers using recursion. 9.Write a program to compute nPr.Use a recursive function fact() to find the factorial. 10.Program to calculate Power of a number ( eg pow(2,3)) 11.Count the digits of a number. 12.Sum of first n natural numbers.

24.Example programs using Recursion

1.Print the factorial of a number using recursion def fact(n):      if n==0:           return 1      else:           return n*fact(n-1) n=int(input('Enter n..:')) x=fact(n) print("factorial of ..",n ," ..is.. ",x) output: Enter n..:5 factorial of .. 5  ..is....120 2.Program to print n’th Fibonacci number def fib(n):   if n <= 1:       return n   else:      return fib(n-1) + fib(n-2) n=int(input('Enter n…')) x=fib(n) print (n,"th Fibonacci number is...",x) output: Enter n…5 5 th Fibonacci number is... 5 3.Program to find sum of the digits of a number def sumd(n):      if n == 0:           return 0     e lse:           return n%10 + sumd(n//10) n=int(input('Enter n')) s=sumd(n) print ("sum of the digits of ..",n ," ..is.. ",s) output: Enter n...123 sum of the digits of .. 123  ..is..  6 4.Program to convert decimal number to binary using recursion def decb(n):      if n==0:           return 0

23.Recursion

Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller sub problems until you get to a small enough problem so that it can be solved trivially. Usually recursion involves a function calling itself. While it may not seem like much on the surface, recursion allows us to write elegant solutions to problems that may otherwise be very difficult to program. Recursion is the process of defining something in terms of itself . It is legal for one function to call another, and you have seen several examples of that. It is also legal for a function to call itself. It may not be obvious why that is a good thing, but it turns out to be one of the most magical and interesting things a program can do. For example, look at the following function: def countdown(n):      if n == 0:           print("Blastoff!")      else:           print(n)           countdown(n-1) A call to this function countdown(5) will print 5 4 3 2 1 Blastoff! Ad

22.Programming Assignment 5 ( using functions)

Now you can write the following programs using simple functions 1.Write a function big(a,b) which will print the biggest of two number. 2.Write a python function to find the area of a circle.( university question) 3.Write a program asks users for the temperature in Fahrenheit (F) and prints the temperature in Celsius. (Conversion: Celsius = (F - 32) * 5/9.0). Use a function convert which takes one argument F and returns C. 4. Write a Python function, odd, that takes in one number and returns True when the number is odd and False otherwise. 5. Write a Python function, fourthPower( ), that takes in one number and returns that value raised to the fourth power. 6.Write a menu driven Python program to simulate a calculator with addition, subtraction, multiplication, division and exponentiation. Use separate function to implement each operation.( university question) 7.Write a program to compute nCr, using a factorial function.(university question) 8.Write a Python program to calculate the a

21.Example programs using functions

Write a python function to find the area of a circle. def circlearea (radius):      area=3.14*(radius**2)     return area #function call r=int(input("Enter radius..")) area=circlearea(r) print("Area of the circle=",area) output Enter radius..2 Area of the circle= 12.56 Compute nCr using a factorial function def fact(n):     f=1     for i in range(1,n+1):         f=f*i     return f print("Program to compute nCr...") n=int(input("Enter n..")) r=int(input("Enter r...")) ncr=fact(n)/(fact(n-r)*fact(r)) print("nCr...",ncr) output Program to compute nCr... Enter n..5 Enter r...3 nCr... 10.0 Python function to extract digits and checks whether all the digits are even or not def digit(n):     flag=1     while n!=0:         d=n%10         if d%2!=0:             flag=0             break         n=n//10     return flag n=int(input("Enter a number::")) if digit(n):     print("All digits are even") else:     print("

20.Global,local and non local variables

Global Variables In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function. Let's see an example of how a global variable is created in Python. Example 1: Create a Global Variable x = "global"  def foo():         print("x inside:", x)   foo()  print("x outside:", x) Output x inside: global  x outside: global Global Variables can be modified in functions with the keyword global X=10 def inc():      global X      X=X+1 def dec():      global X      X=X-1 print(X)  # this will print 10 inc() print (X) # this will print 11 dec() dec() print (X) #this will print 9 Using Global and Local variables in the same code x = "global "   def foo():        global x        y = "local"        x = x * 2        print(x)        print(y)  foo() Output global global  local In t

19.Functions-definition, calling functions,arguments, return value

Image
So far we have only seen the functions which come with Python either in some file (module) or in interpreter itself (built in), but it is also possible for programmer to write their own function(s) and are called User Defined Functions . These functions can then be combined to form a module which can then be used in other programs by importing them. Types of Functions Basically, we can divide functions into the following two types: Built-in functions  - Functions that are built into Python. User-defined functions  - Functions defined by the users themselves. In Python, a function is a group of related statements that performs a specific task.This operation is specified in a function definition. The following are the two simple reasons to write a function Creating a new function gives you an opportunity to name a group of statements. Functions can simplify  a program by hiding a complex computation behind a single command. Creating a new function can make a program smaller by eliminatin

18.Programming Assignment 4( using loops)

Try the Following Programs using loops 1)Print the even numbers between 0 and 50 (use while) 2)Print the odd numbers between 0 and 50 in the reverse order ( use while) 3)Print the series 5,10,15,….,100 (using for loop) 4)Generate the series 1 2 4 7 11 16....n ( use while...read n) 5)Generate the Fibonacci series 0 1 1 2 3 5 8…..n ( use while..read n) 6)Find the factorial of a number ( use for statement do not use built in factorial() function) 7)Print the powers of a number upto 10th power. ( Eg: if n=2 then the program should print 2**0, 2**1, 2**2,2**3,…..,2**10 use for loop) 8)Print the multiplication table of a given number. ( use while) 9)Print the numbers from 1 to 10 and their natural logarithms as a table ( use for) 10)Find the sum of the digits of a number.( use while-university question) 11)Check whether the given 3 digit number is an Armstrong number. (use while Eg:153,370,371,407) 12)Find the factors of a number ( use for) 13)Check whether the given number is prime or not