python programming lecture three strings and functionsfiles.imtschool.com/online/python/lecture...

24
Python Programming Lecture Three Strings and Functions This material is developed by IMTSchool for educational use only All copyrights are reserved

Upload: others

Post on 06-Oct-2020

24 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

Python Programming

Lecture Three

Strings and Functions

This material is developed by IMTSchool for educational use onlyAll copyrights are reserved

Page 2: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• Indexing: string[0]

• Slicing : string[0:3]

• Negative Strings: string[-2]

Strings are Arrays

Page 3: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

LAB 1

Time To

Code

Write a python code that ask the user toenter a sentence and then print it inopposite direction.

Expected Output

Page 4: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

● The lower() method returns the string in lower case

● The upper() method returns the string in upper case

● Length of a string

String Operations

Page 5: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

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

Page 6: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

● 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

Page 7: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

● 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

Page 8: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

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

Page 9: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• 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

Page 10: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• 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

Page 11: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• 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

Page 12: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• function return value

Syntax:

def function_name(Arg_Name):

<Spaces> return Return_Value

Example:

Call:

Function Return

Page 13: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• 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

Page 14: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• 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

Page 15: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• 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

Page 16: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

• 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

Page 17: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

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

Page 18: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

● 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

Page 19: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

● 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

Page 20: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

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

Page 21: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

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

Page 22: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

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.

Page 23: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

The End …

Other types of operators will be discussed later on, …..

Any questions

… ?

Page 24: Python Programming Lecture Three Strings and Functionsfiles.imtschool.com/Online/Python/Lecture 3/Lecture_3.pdf · LAB 1 Time To Code Write a python code that ask the user to enter

This material is developed by IMTSchool for educational use onlyAll copyrights are reserved

www.imtschool.com

ww.facebook.com/imaketechnologyschool/