0% found this document useful (0 votes)
66 views

03 Python Programming Basics Part II

This document covers various Python programming concepts including decision making, loops (while and for), loop control statements (break and continue), functions, and file input/output. It discusses the basics of if/else statements, using break and continue in loops, defining functions, opening and closing files, reading and writing files, and using os module methods like rename(), remove(), mkdir(), and rmdir(). The document is intended as a part 2 of Python basics and is written by Sanka Sandamal.

Uploaded by

Ridee Reka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

03 Python Programming Basics Part II

This document covers various Python programming concepts including decision making, loops (while and for), loop control statements (break and continue), functions, and file input/output. It discusses the basics of if/else statements, using break and continue in loops, defining functions, opening and closing files, reading and writing files, and using os module methods like rename(), remove(), mkdir(), and rmdir(). The document is intended as a part 2 of Python basics and is written by Sanka Sandamal.

Uploaded by

Ridee Reka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Python Programming

Basics Part II

By:
Sanka Sandamal
Decision Making
- Decision-making is the anticipation of
conditions occurring during the execution
of a program and specified actions taken
according to the conditions.
- Decision structures evaluate multiple
expressions, which produce TRUE or
FALSE as the outcome. You need to
determine which action to take and
which statements to execute if the
outcome is TRUE or FALSE otherwise.

Python Programming By: Sanka Sandamal


Loops
- In general, statements are executed
sequentially − The first statement in a
function is executed first, followed by the
second, and so on. There may be a situation
when you need to execute a block of code
several number of times.
- Programming languages provide various
control structures that allow more
complicated execution paths.
- A loop statement allows us to execute a
statement or group of statements multiple
times.

Python Programming By: Sanka Sandamal


While Loop
- A while loop statement in Python
programming language repeatedly
executes a target statement as long as a
given condition is true.
- Here, a key point of the while loop is
that the loop might not ever run. When
the condition is tested and the result is
false, the loop body will be skipped and
the first statement after the while loop
will be executed.

Python Programming By: Sanka Sandamal


The Infinite Loop
- A loop becomes infinite loop if a condition never becomes FALSE.
- You must be cautious when using while loops because of the possibility
that this condition never resolves to a FALSE value.
- This results in a loop that never ends. Such a loop is called an infinite loop.

Python Programming By: Sanka Sandamal


Using else Statement with Loops
- Python supports having an else statement associated with a loop
statement.
- If the else statement is used with a for loop, the else statement is executed
when the loop has exhausted iterating the list.
- If the else statement is used with a while loop, the else statement is
executed when the condition becomes false.

Python Programming By: Sanka Sandamal


for Loop Statements
- The for statement in Python has the ability to iterate over the items of any
sequence, such as a list or a string.
- Syntax
for iterating_var in sequence:
statements(s)

- If a sequence contains an expression list, it is evaluated first. Then, the first


item in the sequence is assigned to the iterating variable iterating_var.
Next, the statements block is executed. Each item in the list is assigned to
iterating_var, and the statement(s) block is executed until the entire
sequence is exhausted.

Python Programming By: Sanka Sandamal


Loop Control Statements
- The Loop control statements change the execution from its normal
sequence.

‘break’ Statement
- The break statement is used for premature termination of the current
loop.
- After abandoning the loop, execution at the next statement is resumed.
- The break statement can be used in both while and for loops.

Python Programming By: Sanka Sandamal


‘break’ Statement (Cont.)

Python Programming By: Sanka Sandamal


‘continue’ statement
- The continue statement in Python returns the control to the beginning of
the current loop.
- When encountered, the loop starts next iteration without executing the
remaining statements in the current iteration.
- The continue statement can be used in both while and for loops.

Python Programming By: Sanka Sandamal


Functions
- A function is a block of organized, reusable code that is used to perform a
single, related action.
- As you already know, 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
- 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 code block within every function starts with a colon (:) and is
indented.

Python Programming By: Sanka Sandamal


Files I/O

Python Programming By: Sanka Sandamal


Reading Keyboard Input
- The input([prompt]) function is equivalent to raw_input, except that it
assumes that the input is a valid Python expression and returns the
evaluated result to you.

The open Function


- Before you can read or write a file, you have to open it using Python's
built-in open() function. This function creates a file object, which would be
utilized to call other support methods associated with it.

Python Programming By: Sanka Sandamal


File Access Modes
- r
Opens a file for reading only. This is the default mode.

- rb
Opens a file for reading only in binary format.

- r+
Opens a file for both reading and writing.

- rb+
Opens a file for both reading and writing in binary format.

- W
Opens a file for writing only. Overwrites the file if the file exists.

Python Programming By: Sanka Sandamal


File Access Modes (Cont.)
- wb
Opens a file for writing only in binary format.

- w+
Opens a file for both writing and reading.

- wb+
Opens a file for both writing and reading in binary format.

- a
Opens a file for appending.

- ab
Opens a file for appending in binary format.

Python Programming By: Sanka Sandamal


File Access Modes (Cont.)
- a+
Opens a file for both appending and reading.

- ab+
Opens a file for both appending and reading in binary format.

Python Programming By: Sanka Sandamal


The close() Method
- The close() method of a file object flushes any unwritten information and
closes the file object, after which no more writing can be done.
- Python automatically closes a file when the reference object of a file is
reassigned to another file. It is a good practice to use the close() method
to close a file.

Python Programming By: Sanka Sandamal


The write() Method
- The write() method writes any string to an open file. It is important to note
that Python strings can have binary data and not just text.
- The write() method does not add a newline character ('\n') to the end of
the string

Python Programming By: Sanka Sandamal


The read() Method
- The read() method reads a string from an open file.

- Here, passed parameter is the number of bytes to be read from the


opened file.

Python Programming By: Sanka Sandamal


The rename() Method
- Python os module provides methods that help you perform file-processing
operations, such as renaming and deleting files.
- To use this module, you need to import it first and then you can call any
related functions.

The remove() Method


- You can use the remove() method to delete files by supplying the name of
the file to be deleted as the argument.

Python Programming By: Sanka Sandamal


The mkdir() Method
- You can use the mkdir() method of the os module to create directories in
the current directory. You need to supply an argument to this method,
which contains the name of the directory to be created.

The rmdir() Method


- The rmdir() method deletes the directory, which is passed as an argument
in the method.

Python Programming By: Sanka Sandamal


The End!
Python Programming
Basics Part II

Thank You.
By:
Sanka Sandamal

You might also like