python programming lecture three strings and functionsfiles.imtschool.com/online/python/lecture...
TRANSCRIPT
Python Programming
Lecture Three
Strings and Functions
This material is developed by IMTSchool for educational use onlyAll copyrights are reserved
• Indexing: string[0]
• Slicing : string[0:3]
• Negative Strings: string[-2]
Strings are Arrays
LAB 1
Time To
Code
Write a python code that ask the user toenter a sentence and then print it inopposite direction.
Expected Output
● The lower() method returns the string in lower case
● The upper() method returns the string in upper case
● Length of a string
String Operations
LAB 2
Time ToCode
Write a python code that handle thefollowing login system:
If the data entered is correct, the systemshall show a welcome message, if not thesystem will print incorrect entry. Notingthat the user can enter his name in anycombination of letter cases
Expected Output
User Name Password
Ahmed 1394
Ali 6078
Amr 9345
● The replace() method replaces a string with another string and it is case
sensitive
● The format() method reformats a string by replacing {} by a value or parameter
value
String Operations
● The count() method returns the number of times a specified value occurs in a
string, return -1 if not found
● The strip() method returns a trimmed version of the string by removing any
spaces at beginning or at the end of a string
String Operations
LAB 3
Time To
Code
Write a python code that ask the user toenter a string and a word to search on it,the system will count how many timesthe word is existing in the string.
The system shall be:1- Case sensitive2- Find whole word only
Expected Output
• A function is a block of code which only runs when it is called. You can pass data,
known as parameters, into a function or to call without passing any date. A
function also can return data as a result.
Syntax:
def function_name():
<Spaces> function body
Example:
Call:
Functions in Python
• Defined number of arguments
Syntax:
def function_name(Arg_Name):
<Spaces> function body
Example:
Call:
Note: function must be called with the correct number of arguments unless default
value is used.
Function Arguments
• Argument has default value
Syntax:
def function_name(Arg_Name):
<Spaces> function body
Example:
Call:
Note: If the an argument is passed it would be used, if not, the default value would be
used.
Function Arguments
• function return value
Syntax:
def function_name(Arg_Name):
<Spaces> return Return_Value
Example:
Call:
Function Return
• Notes:
1- If a function has a return, it is optional to receive it or not.
2- If a function has no return, and you tried to receive a value you will get None Value.
Example:
Output:
Function Return
• Unarranged arguments passing:
If you have a function that takes 3 arguments like that:
You can pass the arguments to it in unordered way like that:
The output is:
Function Arguments
• Variables that are created outside of a function are known as global variables.
Global variables can be used by everyone, both inside of functions and outside.
• Variables that are created inside a function is called local variable and it can be
used only inside the function scope. If you create a variable with the same name of
a global variable inside a function, this variable will be local, and can only be used
inside the function. The global variable with the same name will remain as it was,
global and with the original value.
Global Vs Local Variables
Output
• global keyword is used for:
○ use a global variable that is defined before into the function scope
○ define a new global variable inside a function that could be used anywhere
outside the function
global keyword
Output
A module is a code library. Simple it is a file containing a set of functions you want to
include in your application. To create a module just save the code you want in a file
with the file extension .py
Example: define the following function a file and save it as library.py
Python Module
● To import a module write the import keyword then the name of the module.
● To use a defined function in a module, use the following syntax:
module.functionName()
Example:
Output:
Module Import
● You can specify a certain function to import from a module and then use it like
an internal function.
Syntax:
from Module_Name import Function_Name
Example:
Output:
Module Import
LAB 4
Time To
Code
Write a python module the provide the following bit mathematical operations:1- SET_BIT -> For setting certain bit to 12- CLR_BIT -> For clearing clearing certain bit to 03- TOG_BIT -> For toggling certain bit4- GET_BIT -> For getting certain bit value
LAB 5
Time To
Code
Write a function take a string of odd length greater 7, return a string made of the middle three chars of a given String
LAB 6
Time To
Code
Write a function to check a String characters balance or not
We’ll say that a String s1 and s2 is balanced if all the chars in the string1 are
there in s2. characters position doesn’t matter.
The End …
Other types of operators will be discussed later on, …..
Any questions
… ?
This material is developed by IMTSchool for educational use onlyAll copyrights are reserved
www.imtschool.com
ww.facebook.com/imaketechnologyschool/