Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

UNIT II - Deep Dive Into Python

Python basics and it's functions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

UNIT II - Deep Dive Into Python

Python basics and it's functions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT II

Deep Dive into Python


Boolean
❖ Boolean values are the two constant objects False and True.
❖ They are used to represent truth values (other values can also be considered
false or true).
❖ A string in Python can be tested for truth value.
❖ my_string = "Hello World"
❖ my_string.isalnum() #check if all char are numbers
❖ my_string.isalpha() #check if all char in the string are alphabetic
❖ my_string.isdigit() #test if string contains digits
List
❖ Lists are exible as:
❖ They have no xed size (meaning we do not have to specify how big a list will be).
❖ They have no xed type constraint.
❖ Some of the basic list methods used in python are:
❖ append
❖ pop
❖ sort
❖ reverse
fl
fi
fi
Nested List
❖ A great feature of Python data structures is that they support nesting.
List comprehensions
❖ List comprehensions provide a concise way to create lists.
❖ It consists of brackets containing an expression followed by a ‘for’ clause, then zero or
more ‘for’ or ‘if’ clauses.
❖ The basic syntax is:
❖ [expression for item in list if conditional]
❖ This is equivalent to:
❖ for item in list:
❖ if conditional:
❖ expression
Dictionaries
❖ A dictionary is a collection that is unordered, changeable, and indexed.
❖ In Python, dictionaries are written with curly brackets, and they have keys
and values. A dictionary can be constructed in the following manner:
Dictionaries
❖ We can also create keys by assignment.
❖ For instance, if we started off with an empty dictionary, we could continually add to it:
Dictionaries

❖ A few dictionary methods include:


❖ d.keys()
❖ d.values()
❖ d.items()
Tuples
❖ Tuples are very similar to lists. However, unlike lists, they are immutable, meaning they
cannot be changed.
❖ You would use tuples to present things that should not be changed, such as days of the
week, or dates on a calendar.
❖ The construction of a ‘tuples’ uses () with elements separated by commas.
Tuples
❖ Example of tuple unpacking:
❖ During the ‘for’ loop, we will be unpacking the tuple inside of a sequence and we can
access the individual items inside that tuple.
Python Fundamentals: Conditional Statements
Introduction to the IF statement
Python Fundamentals: Conditional Statements
Add an ELSE statement
Python Fundamentals: Conditional Statements
Else if, for Brief - ELIF
Python Fundamentals: While Loop
❖ Syntax:
❖ while test:
❖ code statements
❖ else:
❖ nal code statements
fi
break, continue, and pass statements

❖ We can use ‘break’, ‘continue’, and ‘pass’


statements in our loops to add additional
functionality for various cases. The three
statements are de ned as:
❖ break: Breaks out of the current closest
enclosing loop
❖ continue: Goes to the top of the closest
enclosing loop
❖ pass: Does nothing at all
fi
Python Fundamentals: Functions
De ning a Function in Python
fi
Python Fundamentals: Functions
Creating a Function with a Parameter

You might also like