Testing strings String class in python has various

  • Slides: 21
Download presentation
Testing strings • String class in python has various inbuilt methods which allows to

Testing strings • String class in python has various inbuilt methods which allows to check for different types of strings. Method Name Method Description isalnum() Returns True if string is alphanumeric isalpha() Returns True if string contains only alphabets isdigit() Returns True if string contains only digits isidentifier() Return True is string is valid identifier islower() Returns True if string is in lowercase isupper() Returns True if string is in uppercase isspace() Returns True if string contains only whitespace

 • print("my string", end="n") #this is default behavior • print("my string", end="") #

• print("my string", end="n") #this is default behavior • print("my string", end="") # print string without a newline • print("my string", end="foo") # now print() will print foo after every string

Searching for Substrings METHOD NAME METHODS DESCRIPTION: endswith( str) Returns True if strings ends

Searching for Substrings METHOD NAME METHODS DESCRIPTION: endswith( str) Returns True if strings ends with substring s 1 startswith(str) Returns True if strings starts with substring s 1 count(substring) Returns number of occurrences of substring the string find(s 1) Returns lowest index from where s 1 starts in the string, if string not found returns -1 rfind(s 1) Returns highest index from where s 1 starts in the string, if string not found returns -1

Functions • A function is a block of organized, reusable code that is used

Functions • A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. • Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Defining a Function You can define functions to provide the required functionality. Here are

Defining a Function You can define functions to provide the required functionality. Here are simple rules to define a function in Python. • Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses. • The first statement of a function can be an optional statement - the documentation string of the function or docstring. • The code block within every function starts with a colon (: ) and is indented. • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Syntax def functionname( parameters ): "function_docstring" function_suite return [expression] Example def nothing(str): print (str)

Syntax def functionname( parameters ): "function_docstring" function_suite return [expression] Example def nothing(str): print (str) return nothing('anvesh')

Calling a Function • Once the basic structure of a function is finalized, you

Calling a Function • Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt.

Pass by reference vs value • All parameters (arguments) in the Python language are

Pass by reference vs value • All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.

Default Parameter Value • If we call the function without parameter, it uses the

Default Parameter Value • If we call the function without parameter, it uses the default value. And to let a function return a value, use the return statement:

 • Keyword arguments are related to the function calls. When you use keyword

• Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.

Variable length arguments def vla(branch, *deptstrength): print("n", branch, "No. of students are: ") for

Variable length arguments def vla(branch, *deptstrength): print("n", branch, "No. of students are: ") for x in dept: print(x) vla("IT", 120, "110", 97, "107") vla("AE", "aero 120", 60)

lambda • A lambda function is a small anonymous function. • Lambda or anonymous

lambda • A lambda function is a small anonymous function. • Lambda or anonymous functions are so called because they are not declared as other functions using the def keyword. Rather, they are declared using the lambda keyword. Lambda functions are throw-away functions, because they are just used where they have been created. • Lambda functions contain only a single line. Its syntax will be as follows: • lambda arguments : expression

Lambda - > Syntax & Example lambda [arg 1 [, arg 2, . .

Lambda - > Syntax & Example lambda [arg 1 [, arg 2, . . . argn]]: expression

 • The arguments contain a comma separated list of arguments and the expression

• The arguments contain a comma separated list of arguments and the expression is an arithmetic expression that uses these arguments. The function can be assigned to a variable to give it a name. • Example #lambda or anonymouse function n=lambda x, y: x**y x=int(input("Enter value of x : ")) y=int(input("Enter value of y : ")) #function call in the print() function print(x, "power", y, "is", n(x, y))

Properties of Lambda or Anonymous functions: • Lambda functions have no name • Lambda

Properties of Lambda or Anonymous functions: • Lambda functions have no name • Lambda functions can take any number of arguments • Lambda functions can just return a single value • Lambda functions cannot access variables other than in their parameter list. • Lambda functions cannot even access global variables

Anonymous Functions These functions are called anonymous because they are not declared in the

Anonymous Functions 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. • 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.

Lambda - > Syntax & Example lambda [arg 1 [, arg 2, . .

Lambda - > Syntax & Example lambda [arg 1 [, arg 2, . . . argn]]: expression

Scope of Variables • All variables in a program may not be accessible at

Scope of Variables • All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable. 1. Global variables 2. Local variables

Example program for Scope of variables

Example program for Scope of variables