The document discusses various Python flow control statements like if/else, while loops, for loops, break and continue. It provides examples and explanations of how each statement works. Key flow control statements covered include if/else for conditional execution, while loops for repetitive execution as long as a condition is true, for loops for executing a block of code a specific number of times, and break/continue for early loop termination or skipping iterations. It also discusses importing modules and functions from the Python standard library.
This chapter discusses repetition and looping control structures in Java, including while, for, and do-while loops. It provides examples of using these loops, such as printing numbers in a range or calculating Fibonacci numbers recursively. The chapter also covers statements for early loop exit like break and continue, and nesting control structures for increased complexity.
Types of Statements in Python Programming LanguageExplore Skilled
The slide describes the types of flow control statements in Python including conditional statement , loop statement , control statement such as break and continue .
Operators and expressions are fundamental concepts in Python programming. The document discusses various types of operators used to manipulate operands, including arithmetic, comparison, assignment, logical, bitwise, and membership operators. It also covers expressions, which are combinations of operators and operands that evaluate to a value. Several types of expressions are described, such as constant, arithmetic, integral, floating, relational, logical, bitwise, and combinational expressions. Control flow statements like if, if-else, if-elif-else are also covered, along with looping using for and while loops and the break, continue, and pass statements.
The document discusses various control structures and functions used in Arduino programming including decision making structures like if, else if, else statements and switch case statements. It also covers different types of loops like while, do-while and for loops that allow repeating blocks of code. Functions are described as reusable blocks of code that perform tasks and help organize a program. Strings can be implemented as character arrays or using the String class, and various string functions are provided to manipulate and work with strings.
The document discusses various programming concepts in Python including conditional statements, loops, functions, and random number generation. Conditional statements like if, if-else, and nested if are used to perform tasks based on conditions. Loops like while and for are used for sequential traversal and repetition. Functions allow reusable blocks of code and can accept parameters and return values. The random and randint functions in Python can generate random floating numbers and integers within a given range for probabilistic behavior.
This document discusses Python loops and string manipulation. It covers while loops, using a loop counter to repeat code a specified number of times. It also discusses slicing strings to access characters or substrings, checking if a string contains a character, and calling string methods like lower(), upper(), and replace() to manipulate strings.
The document discusses Python control structures including if-else statements, indentation, and loops. It provides examples of if statements, if-else statements, nested if statements, and elif statements to test conditions and execute code blocks accordingly. It also discusses for loops and the range() function to iterate through sequences and generate number sequences to repeat code. Loops simplify problems by allowing code to repeat without rewriting and help traverse data structures.
This document discusses repetition structures in C programming, including while, for, and do-while loops. It covers the basic components of loops, different types of loops, and common errors. Key topics include using while loops to compute sums and averages, using for loops with initializing, testing, and altering expressions, applying different loop programming techniques, and nesting loops within each other.
This chapter discusses repetition and looping control structures in Java, including while, for, and do-while loops. It provides examples of using these loops, such as printing numbers in a range or calculating Fibonacci numbers recursively. The chapter also covers statements for early loop exit like break and continue, and nesting control structures for increased complexity.
Types of Statements in Python Programming LanguageExplore Skilled
The slide describes the types of flow control statements in Python including conditional statement , loop statement , control statement such as break and continue .
Operators and expressions are fundamental concepts in Python programming. The document discusses various types of operators used to manipulate operands, including arithmetic, comparison, assignment, logical, bitwise, and membership operators. It also covers expressions, which are combinations of operators and operands that evaluate to a value. Several types of expressions are described, such as constant, arithmetic, integral, floating, relational, logical, bitwise, and combinational expressions. Control flow statements like if, if-else, if-elif-else are also covered, along with looping using for and while loops and the break, continue, and pass statements.
The document discusses various control structures and functions used in Arduino programming including decision making structures like if, else if, else statements and switch case statements. It also covers different types of loops like while, do-while and for loops that allow repeating blocks of code. Functions are described as reusable blocks of code that perform tasks and help organize a program. Strings can be implemented as character arrays or using the String class, and various string functions are provided to manipulate and work with strings.
The document discusses various programming concepts in Python including conditional statements, loops, functions, and random number generation. Conditional statements like if, if-else, and nested if are used to perform tasks based on conditions. Loops like while and for are used for sequential traversal and repetition. Functions allow reusable blocks of code and can accept parameters and return values. The random and randint functions in Python can generate random floating numbers and integers within a given range for probabilistic behavior.
This document discusses Python loops and string manipulation. It covers while loops, using a loop counter to repeat code a specified number of times. It also discusses slicing strings to access characters or substrings, checking if a string contains a character, and calling string methods like lower(), upper(), and replace() to manipulate strings.
The document discusses Python control structures including if-else statements, indentation, and loops. It provides examples of if statements, if-else statements, nested if statements, and elif statements to test conditions and execute code blocks accordingly. It also discusses for loops and the range() function to iterate through sequences and generate number sequences to repeat code. Loops simplify problems by allowing code to repeat without rewriting and help traverse data structures.
This document discusses repetition structures in C programming, including while, for, and do-while loops. It covers the basic components of loops, different types of loops, and common errors. Key topics include using while loops to compute sums and averages, using for loops with initializing, testing, and altering expressions, applying different loop programming techniques, and nesting loops within each other.
This chapter discusses control structures in Java programming, including selection structures like if/else statements and switch statements. It covers logical and relational operators, comparing strings, and how to properly use the syntax for if/else and switch statements. Examples are provided to demonstrate how to use these control structures to write programs that can evaluate conditions and branch the program flow accordingly.
This document discusses different types of loops in Java including while, do-while, and for loops. It explains the syntax and flow control of each loop type. Examples are provided to demonstrate how to use loops appropriately. Common mistakes like infinite loops and off-by-one errors are highlighted. The break and continue keywords are also explained. Finally, the document briefly introduces methods in Java.
While-For-loop in python used in collegessuser7a7cd61
This document discusses loops in Python programming. It covers for loops, range functions, break/continue/pass statements, booleans, while loops, and nested loops. Some key points:
- For loops iterate over sequences like lists and strings, while loops repeat code while a condition is true.
- Range functions generate numeric sequences for use in for loops. They can specify a start, end, and optional step size.
- Break/continue/pass are loop control statements - break exits the loop, continue skips to the next iteration.
- Boolean values are True or False and are used to check conditions in loops.
- Nested loops involve loops within other loops, like a for loop inside a for loop.
The document contains code snippets and explanations about different concepts in Java programming including accepting user input, option panes, control statements, boolean operators, and conditional operators. The code examples demonstrate how to take input from the user, display messages and get input using option panes, use if/else, switch statements, and boolean/conditional operators to control program flow based on conditions.
Python functions allow breaking down code into reusable blocks to perform tasks. There are several types of functions including built-in, user-defined, and anonymous functions. User-defined functions are defined using the def keyword and can take in parameters. Functions can return values using the return statement. Functions are called by their name along with any arguments. Arguments are passed into parameters and can be positional, keyword, or have default values. Functions increase code reuse and readability.
Object oriented programming system with C++msharshitha03s
This document provides an overview of C++ control statements, functions, and storage classes. It discusses various loops like while, for, and do-while loops. It also covers decision making statements such as if-else, if-else-if-else, switch statements, and unconditional statements like break, continue, and goto. The document then discusses functions, recursion, and inline functions. Finally, it summarizes different storage classes in C++ like auto, register, static, external, and mutable and provides examples of each.
The document discusses different types of loops and conditional statements in programming languages. It explains the if, if-else, and switch conditional statements, providing examples of each. It also covers different types of loops - while, do-while, for, and nested loops. Examples are given to illustrate the syntax and usage of each loop and conditional statement.
Here is a Java program that takes integer input from the user between 0-100 and prints the number in English:
import java.util.Scanner;
public class NumberToWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number between 0-100: ");
int num = sc.nextInt();
if(num < 0 || num > 100) {
System.out.println("Number is out of range!");
return;
}
String word = numberToWord(num);
System.out.println(word);
}
public static String numberToWord(int number)
The document discusses various looping constructs in Java including for, while, do-while loops as well as decision making statements like if, if-else, switch. It provides the syntax and examples of each. The key loops covered are for, which allows looping a specific number of times, while which checks a condition before running the loop body, and do-while which runs the body at least once. The document also discusses break and continue keywords that can be used within loops. For decision making, it explains if, if-else-if-else for multiple conditions, and switch for equality checks. Nested if statements are also covered.
Android Application Development - Level 3Isham Rashik
Final level of the Android Application Development series. With this tutorial documentation, this will enable mobile developers to gain complete basic knowledge of Android Development. After completing all 3 of the levels, the developers can proceed with intermediary and advanced concepts.
This chapter discusses repetition control structures in C++ like while, for, and do-while loops. It explains how to construct count-controlled, sentinel-controlled, and flag-controlled loops. It also covers break and continue statements, and how to create nested loops to produce patterns like a triangle of increasing asterisks.
This document discusses decision making and loops in Python. It describes the different types of decision making statements like IF, IF-ELSE, and nested IF statements. It also explains while and for loops used to repeat a block of code. Additionally, it covers loop control statements such as break, continue, and pass that alter normal loop execution.
Python is a general purpose programming language that can be used for both programming and scripting. It is an interpreted language, meaning code is executed line by line by the Python interpreter. Python code is written in plain text files with a .py extension. Key features of Python include being object-oriented, using indentation for code blocks rather than brackets, and having a large standard library. Python code can be used for tasks like system scripting, web development, data analysis, and more.
This document provides an overview of Python basics including statements, expressions, loops, strings, functions, and more. It discusses what a program is and how it runs. It explains how to get input from the user, import modules like math, use assignment statements, and print output. It also covers syntax, side effects vs returns, whitespace, operators, loops, range function, strings, indexes, basic string operations, len function, and functions. The document is from a Python training institute in Bangalore and is intended to teach Python fundamentals.
Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.
1. The Python if statement is used to execute code conditionally based on the evaluation of an expression. It can be used with simple conditions, if-else statements, and nested if statements.
2. Python if-else statements allow executing one block of code if a condition is true and another block if the condition is false.
3. Nested if statements in Python allow checking multiple conditions, with inner if statements executing only if the outer conditions are met.
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
This chapter discusses control structures in Java programming, including selection structures like if/else statements and switch statements. It covers logical and relational operators, comparing strings, and how to properly use the syntax for if/else and switch statements. Examples are provided to demonstrate how to use these control structures to write programs that can evaluate conditions and branch the program flow accordingly.
This document discusses different types of loops in Java including while, do-while, and for loops. It explains the syntax and flow control of each loop type. Examples are provided to demonstrate how to use loops appropriately. Common mistakes like infinite loops and off-by-one errors are highlighted. The break and continue keywords are also explained. Finally, the document briefly introduces methods in Java.
While-For-loop in python used in collegessuser7a7cd61
This document discusses loops in Python programming. It covers for loops, range functions, break/continue/pass statements, booleans, while loops, and nested loops. Some key points:
- For loops iterate over sequences like lists and strings, while loops repeat code while a condition is true.
- Range functions generate numeric sequences for use in for loops. They can specify a start, end, and optional step size.
- Break/continue/pass are loop control statements - break exits the loop, continue skips to the next iteration.
- Boolean values are True or False and are used to check conditions in loops.
- Nested loops involve loops within other loops, like a for loop inside a for loop.
The document contains code snippets and explanations about different concepts in Java programming including accepting user input, option panes, control statements, boolean operators, and conditional operators. The code examples demonstrate how to take input from the user, display messages and get input using option panes, use if/else, switch statements, and boolean/conditional operators to control program flow based on conditions.
Python functions allow breaking down code into reusable blocks to perform tasks. There are several types of functions including built-in, user-defined, and anonymous functions. User-defined functions are defined using the def keyword and can take in parameters. Functions can return values using the return statement. Functions are called by their name along with any arguments. Arguments are passed into parameters and can be positional, keyword, or have default values. Functions increase code reuse and readability.
Object oriented programming system with C++msharshitha03s
This document provides an overview of C++ control statements, functions, and storage classes. It discusses various loops like while, for, and do-while loops. It also covers decision making statements such as if-else, if-else-if-else, switch statements, and unconditional statements like break, continue, and goto. The document then discusses functions, recursion, and inline functions. Finally, it summarizes different storage classes in C++ like auto, register, static, external, and mutable and provides examples of each.
The document discusses different types of loops and conditional statements in programming languages. It explains the if, if-else, and switch conditional statements, providing examples of each. It also covers different types of loops - while, do-while, for, and nested loops. Examples are given to illustrate the syntax and usage of each loop and conditional statement.
Here is a Java program that takes integer input from the user between 0-100 and prints the number in English:
import java.util.Scanner;
public class NumberToWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number between 0-100: ");
int num = sc.nextInt();
if(num < 0 || num > 100) {
System.out.println("Number is out of range!");
return;
}
String word = numberToWord(num);
System.out.println(word);
}
public static String numberToWord(int number)
The document discusses various looping constructs in Java including for, while, do-while loops as well as decision making statements like if, if-else, switch. It provides the syntax and examples of each. The key loops covered are for, which allows looping a specific number of times, while which checks a condition before running the loop body, and do-while which runs the body at least once. The document also discusses break and continue keywords that can be used within loops. For decision making, it explains if, if-else-if-else for multiple conditions, and switch for equality checks. Nested if statements are also covered.
Android Application Development - Level 3Isham Rashik
Final level of the Android Application Development series. With this tutorial documentation, this will enable mobile developers to gain complete basic knowledge of Android Development. After completing all 3 of the levels, the developers can proceed with intermediary and advanced concepts.
This chapter discusses repetition control structures in C++ like while, for, and do-while loops. It explains how to construct count-controlled, sentinel-controlled, and flag-controlled loops. It also covers break and continue statements, and how to create nested loops to produce patterns like a triangle of increasing asterisks.
This document discusses decision making and loops in Python. It describes the different types of decision making statements like IF, IF-ELSE, and nested IF statements. It also explains while and for loops used to repeat a block of code. Additionally, it covers loop control statements such as break, continue, and pass that alter normal loop execution.
Python is a general purpose programming language that can be used for both programming and scripting. It is an interpreted language, meaning code is executed line by line by the Python interpreter. Python code is written in plain text files with a .py extension. Key features of Python include being object-oriented, using indentation for code blocks rather than brackets, and having a large standard library. Python code can be used for tasks like system scripting, web development, data analysis, and more.
This document provides an overview of Python basics including statements, expressions, loops, strings, functions, and more. It discusses what a program is and how it runs. It explains how to get input from the user, import modules like math, use assignment statements, and print output. It also covers syntax, side effects vs returns, whitespace, operators, loops, range function, strings, indexes, basic string operations, len function, and functions. The document is from a Python training institute in Bangalore and is intended to teach Python fundamentals.
Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.
1. The Python if statement is used to execute code conditionally based on the evaluation of an expression. It can be used with simple conditions, if-else statements, and nested if statements.
2. Python if-else statements allow executing one block of code if a condition is true and another block if the condition is false.
3. Nested if statements in Python allow checking multiple conditions, with inner if statements executing only if the outer conditions are met.
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
This presentation includes basic of PCOS their pathology and treatment and also Ayurveda correlation of PCOS and Ayurvedic line of treatment mentioned in classics.
Main Java[All of the Base Concepts}.docxadhitya5119
This is part 1 of my Java Learning Journey. This Contains Custom methods, classes, constructors, packages, multithreading , try- catch block, finally block and more.
Assessment and Planning in Educational technology.pptxKavitha Krishnan
In an education system, it is understood that assessment is only for the students, but on the other hand, the Assessment of teachers is also an important aspect of the education system that ensures teachers are providing high-quality instruction to students. The assessment process can be used to provide feedback and support for professional development, to inform decisions about teacher retention or promotion, or to evaluate teacher effectiveness for accountability purposes.
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Dr. Vinod Kumar Kanvaria
Exploiting Artificial Intelligence for Empowering Researchers and Faculty,
International FDP on Fundamentals of Research in Social Sciences
at Integral University, Lucknow, 06.06.2024
By Dr. Vinod Kumar Kanvaria
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...PECB
Denis is a dynamic and results-driven Chief Information Officer (CIO) with a distinguished career spanning information systems analysis and technical project management. With a proven track record of spearheading the design and delivery of cutting-edge Information Management solutions, he has consistently elevated business operations, streamlined reporting functions, and maximized process efficiency.
Certified as an ISO/IEC 27001: Information Security Management Systems (ISMS) Lead Implementer, Data Protection Officer, and Cyber Risks Analyst, Denis brings a heightened focus on data security, privacy, and cyber resilience to every endeavor.
His expertise extends across a diverse spectrum of reporting, database, and web development applications, underpinned by an exceptional grasp of data storage and virtualization technologies. His proficiency in application testing, database administration, and data cleansing ensures seamless execution of complex projects.
What sets Denis apart is his comprehensive understanding of Business and Systems Analysis technologies, honed through involvement in all phases of the Software Development Lifecycle (SDLC). From meticulous requirements gathering to precise analysis, innovative design, rigorous development, thorough testing, and successful implementation, he has consistently delivered exceptional results.
Throughout his career, he has taken on multifaceted roles, from leading technical project management teams to owning solutions that drive operational excellence. His conscientious and proactive approach is unwavering, whether he is working independently or collaboratively within a team. His ability to connect with colleagues on a personal level underscores his commitment to fostering a harmonious and productive workplace environment.
Date: May 29, 2024
Tags: Information Security, ISO/IEC 27001, ISO/IEC 42001, Artificial Intelligence, GDPR
-------------------------------------------------------------------------------
Find out more about ISO training and certification services
Training: ISO/IEC 27001 Information Security Management System - EN | PECB
ISO/IEC 42001 Artificial Intelligence Management System - EN | PECB
General Data Protection Regulation (GDPR) - Training Courses - EN | PECB
Webinars: https://pecb.com/webinars
Article: https://pecb.com/article
-------------------------------------------------------------------------------
For more information about PECB:
Website: https://pecb.com/
LinkedIn: https://www.linkedin.com/company/pecb/
Facebook: https://www.facebook.com/PECBInternational/
Slideshare: http://www.slideshare.net/PECBCERTIFICATION
Strategies for Effective Upskilling is a presentation by Chinwendu Peace in a Your Skill Boost Masterclass organisation by the Excellence Foundation for South Sudan on 08th and 09th June 2024 from 1 PM to 3 PM on each day.
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
South African Journal of Science: Writing with integrity workshop (2024)
ch2 Python flow control.pdf
1. Flow Control
if, else, elif, while, break,
continue, for
Ranjana Thakuria
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
1
2. Flow Control Statements
if Statement
else Statements
elif Statements
while Loop Statements
break Statements
continue Statements
for Loops and the range() Function
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
2
3. if else Statement
if Statement :
The most common type of flow control statement is the if statement.
An if statement’s clause (that is, the block following the if statement)
will execute if the statement’s condition is True.
The clause is skipped if the condition is False.
else Statements
An if clause can optionally be followed by an else statement.
The else clause is executed only when the if statement’s condition is
False.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
3
4. if statement consists
The if keyword
A condition (that is, an expression that evaluates to True or False)
A colon(:)
Starting on the next line, an indented block of code (called the if clause)
else statement consists
The else keyword
A colon
Starting on the next line, an indented block of code (called the else
clause)
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 4
5. If Example:
if name == 'Alice’:
print('Hi, Alice.')
Flowchart:
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 5
6. If –else Example:
if name == 'Alice’:
print('Hi, Alice.')
else:
print('Hello, stranger.')
Flowchart:
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 6
7. Rules for blocks
There are three rules for blocks.
1. Blocks begin when the
indentation increases.
2. Blocks can contain other
blocks.
3. Blocks end when the
indentation decreases to zero or to
a containing block’s indentation.
Example:
if name == 'Mary’:
print('Hello Mary')
if password == 'swordfish’:
print('Access granted.')
else:
print('Wrong password.')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 7
8. while Loop Statements
• You can make a block of code execute over
and over again with a while statement.
• The code in a while clause will be executed
as long as the while statement’s condition is
True.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 8
9. A while statement always consists:
• The while keyword
• A condition (that is, an expression that evaluates
to True or False)
• A colon
• Starting on the next line, an indented block of
code (called the while clause)
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 9
11. break Statements
• ‘Break’ in Python is a loop control statement.
• There is a shortcut to getting the program execution to
break out of a while loop’s clause early.
• If the execution reaches a break statement, it immediately
exits the while loop’s clause.
• break statement is put inside the loop body (generally
after if condition).
• In code, a break statement simply contains the break
keyword.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 11
12. Example- break:
while True:
print('Please type your
name.’)
name = input()
if name == 'your name’:
break
print('Thank you!')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 12
13. continue Statements
The continue statement is used to skip the rest of the code
inside a loop for the current iteration only.
Loop does not terminate but continues on with the next
iteration.
When the program execution reaches a continue statement,
the program execution immediately jumps back to the start
of the loop and re-evaluates the loop’s condition.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 13
15. for Loops and the range() Function
To execute a block of code only a certain number of
times
It uses for loop statement and the range() function
together
Eg. for i in range(5):
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru
15
16. for always includes the following:
• The for keyword
• A variable name
• The in keyword
• A call to the range() method with up to three
integers passed to it
• A colon
• Starting on the next line, an indented block of
code (called the for clause)
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 16
17. Example:
# display your name five times:
print('My name is')
for i in range(5):
print('Jimmy Five Times (' + str(i) + ')')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 17
18. Example 1:
The Starting, Stopping, and Stepping Arguments to range()-
for i in range(12, 16):
print(i)
Explanation:
*The first argument will be where the for loop’s variable
starts.
*The second argument will be up to, but not including, the
number to stop at.
Output:
12
13
14
15
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 18
19. Example 1:
The Starting, Stopping, and Stepping Arguments to range()-
for i in range(0, 10, 2):
print(i)
:
• The range() function can also be called with three
arguments.
• The first two arguments will be the start and stop
values, and the third will be the step argument.
• The step is the amount that the variable is increased by
after each iteration.
Output:
0
2
4
6
8
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 19
20. Example 3:
for i in range(5, -1, -1):
print(i)
Explanation:
The 3rd arguments to range() function
is -1.So backward propagation can be
achieved.
Output:
5
4
3
2
1
0
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 20
21. Importing Modules
• All Python programs can call a basic set of functions called built-in
functions, including the print(), input(), and len() functions.
• Python also comes with a set of modules called the standard library.
• Each module is a Python program that contains a related group of functions
that can be embedded in your programs.
• For example, the math module has mathematics- related functions.
• The random module has random number–related functions, and so on.
• Before you can use the functions in a module, you must import the module
with an import statement.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 21
22. An import statement consists of the
following:
• The import keyword
• The name of the module
• Optionally, more module names, as long as they
are separated by commas
• Once you import a module, you can use all the
functions of that module.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 22
23. Example : import
1)
import random for i in range(5):
print(random.randint(1, 10))
Note:
The random.randint() is a
function call.
randint() is function name which
is present in the random module.
2)
import random, sys, os, math
3) An alternative form of the import
statement
from random import *
Note:
composed of the from keyword,
followed by the module name, the
import keyword, and a star;
With this form of import statement,
calls to functions in random will not
need the random. prefix.
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 23
24. Ending a Program early with sys.exit()
• This can cause the program to terminate, or exit, by calling the
sys.exit() function.
• Since this function is in the sys module, you have to import sys before
your program can use it.
Example: import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed ' + response + '.')
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 24
25. Bibliography:
1. Al Sweigart,“Automate the Boring Stuff with Python”,1 stEdition, No Starch Press, 2015. (Available
under CC-BY-NC-SA license at https://automatetheboringstuff.com/) (Chapters 1 to 18, except 12) for
lambda functions use this link: https://www.learnbyexample.org/python-lambda-function/
2. Allen B. Downey, “Think Python: How to Think Like a Computer Scientist”, 2 nd Edition, Green Tea
Press, 2015. (Available under CC-BY-NC license at
http://greenteapress.com/thinkpython2/thinkpython2.pdf (Chapters 13, 15, 16, 17, 18) (Download
pdf/html files from the above link)
3. Introduction to Python Programming - 22PLC15B/25B (Module 1) notes Prepared by, Mrs. Divyaraj G
N, Assistant Professor, CSE, SVCE, Bengaluru
Ranjana Thakuria, Astt. Prof, CSE, SVCE, Bengaluru 25