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]]:expressi...

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, requirem...

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 d...

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

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     ...

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   ...

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...