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

ITEC 111 Python 02 - Intro To Python Part 2

This document discusses Python virtual environments and how they allow different Python applications to install different versions of libraries without conflicts. It explains how to create a virtual environment using the venv module and activate/deactivate it. It also provides basic syntax for a Python script and shows how to run a simple Python program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

ITEC 111 Python 02 - Intro To Python Part 2

This document discusses Python virtual environments and how they allow different Python applications to install different versions of libraries without conflicts. It explains how to create a virtual environment using the venv module and activate/deactivate it. It also provides basic syntax for a Python script and shows how to run a simple Python program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Python Introduction part 2

ITEC 111
Python Virtual Environment
While developing an application in Python, one or more libraries may
be required to be installed using the pip utility (e.g., pip3 install
somelib). Moreover, an application (let us say App1) may require a
particular version of the library, say somelib 1.0. At the same time
another Python application (for example App2) may require newer
version of same library say somelib 2.0. Hence by installing a new
version, the functionality of App1 may be compromised because of
conflict between two different versions of same library.
Create new Virtual Environment
This functionality is supported by venv module in standard Python
distribution. Use following commands to create a new virtual
environment.
Activate/Deactivate Virtual Environment
To enable a virtual environment, execute activate.bat in Scripts folder.
Basic Syntax
Let us write a simple Python program in a script which is simple text
file. Python files have extension .py.

Try to run this program as follows


Python Identifiers
• A Python identifier is a name used to identify a variable, function,
class, module or other object. An identifier starts with a letter A to Z
or a to z or an underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
• Python does not allow punctuation characters such as @, $, and %
within identifiers.
• Python is a case sensitive programming language. Thus, Manpower
and manpower are two different identifiers in Python.
Identifier Naming Conventions
• Python Class names start with an uppercase letter. All other
identifiers start with a lowercase letter.
• Starting an identifier with a single leading underscore indicates that
the identifier is private identifier.
• Starting an identifier with two leading underscores indicates a
strongly private identifier.
• If the identifier also ends with two trailing underscores, the identifier
is a language-defined special name.
Reserved Keywords
and as assert
break class continue
def del elif
else except False
finally for from
global if import
in is lambda
None nonlocal not
or pass raise
return True try
while with yield
Python Lines and Indentation
Python programming provides no braces to indicate blocks of code for
class and function definitions or flow control. Blocks of code are
denoted by line indentation, which is rigidly enforced. All statements
within the block must be indented
Quotations in Python
Python accepts single ('), double (") and triple (''' or """) quotes to
denote string literals, as long as the same type of quote starts and ends
the string.
The triple quotes are used to span the string across multiple lines. For
example, all the following are legal
Comments in Python
A comment is a programmer-readable explanation or annotation in the
Python source code. They are added with the purpose of making the
source code easier for humans to understand, and are ignored by
Python interpreter
Python Variables
Data items belonging to different data types are stored in computer's
memory. Computer's memory locations are having a number or
address, internally represented in binary form. Data is also stored in
binary form as the computer works on the principle of binary
representation.
Python Naming Patterns
• Camel case − First letter is a lowercase, but first letter of each
subsequent word is in uppercase. For example: kmPerHour,
pricePerLitre
• Pascal case − First letter of each word is in uppercase. For example:
KmPerHour, PricePerLitre
• Snake case − Use single underscore (_) character to separate words.
For example: km_per_hour, price_per_litre
Python - Data Types
• Data type represents a kind of value and determines what operations
can be done on it
Number Type
Any data item having a numeric value is a number. There are Four
standard number data types in Python. They are integer, floating point,
Boolean and Complex. Each of them have built-in classes in Python
library, called int, float, bool and complex respectively.
In Python, a number is an object of its corresponding class. For
example, an integer number 123 is an object of int class. Similarly, 9.99
is a floating point number, which is an object of float class.
Sequence Types
Sequence is a collection data type. It is an ordered collection of items.
Items in the sequence have a positional index starting with 0. It is
conceptually similar to an array in C or C++. There are three sequence
types defined in Python. String, List and Tuple.
Strings in Python
A string is a sequence of one or more Unicode characters, enclosed in
single, double or triple quotation marks (also called inverted commas).
As long as the same sequence of characters is enclosed, single or
double or triple quotes don’t matter.
List in Python
• In Python, List is an ordered collection of any type of data items. Data
items are separated by comma (,) symbol and enclosed in square
brackets ([]). A list is also a sequence, hence.
• each item in the list has an index referring to its position in the
collection. The index starts from 0.
• The list in Python appears to be similar to array in C or C++. However,
there is an important difference between the two. In C/C++, array is a
homogenous collection of data of similar types. Items in the Python
list may be of different types.
Tuples in Python
• In Python, a Tuple is an ordered collection of any type of data items.
Data items are separated by comma (,) symbol and enclosed in
parentheses or round brackets (). A tuple is also a sequence, hence
each item in the tuple has an index referring to its position in the
collection. The index starts from 0.
The difference between list and tuple
The two sequence types list and tuple appear to be similar except the
use of delimiters, list uses square brackets ([]) while tuple uses
parentheses. However, there is one major difference between list and
tuple.
List is mutable object, whereas tuple is immutable. An object is
immutable means once it is stored in the memory, it cannot be
changed.
Dictionary Type
Python's dictionary is example of mapping type. A mapping object
'maps' value of one object with another. In a language dictionary we
have pairs of word and corresponding meaning. Two parts of pair are
key (word) and value (meaning).
Similarly, Python dictionary is also a collection of key:value pairs. The
pairs are separated by comma and put inside curly brackets {}. To
establish mapping between key and value, the semicolon':' symbol is
put between the two.
Dictionary Type cont…
Each key in a dictionary must be unique, and should be a number,
string or tuple. The value object may be of any type, and may be
mapped with more than one keys (they need not be unique)
Python’s dictionary is not a sequence. It is a collection of items but
each item (key:value pair) is not identified by positional index as in
string, list or tuple.
Set Type
Set is a Python implementation of set as defined in Mathematics. A set
in Python is a collection, but is not an indexed or ordered collection as
string, list or tuple. An object cannot appear more than once in a set,
whereas in List and Tuple, same object can appear more than once.
Comma separated items in a set are put inside curly brackets or braces.
Items in the set collection may be of different data types.
Casting in Python
In manufacturing, casting is the process of pouring a liquefied or
molten metal into a mold, and letting it cool to obtain the desired
shape. In programming, casting refers to converting an object of one
type into another.
In Python there are different data types, such as numbers, sequences,
mappings etc. There may be a situation where, you have the available
data of one type but you want to use it in another form. For example,
the user has input a string but you want to use it as a number. Python
type casting mechanism lets you do that.
int() Function
Python’s built-in int() function converts an integer literal to an integer
object, a float to integer, and a string to integer if the string itself has a
valid integer literal representation.
Using int() with an int object as argument is equivalent to declaring an
int object directly.
float() Function
float() is a built-in function in Python. It returns a float object if the
argument is a float literal, integer or a string with valid floating point
representation.
Using float() with an float object as argument is equivalent to declaring
a float object directly
str() Function
The str() function works the opposite. It surrounds an integer or a float
object with quotes (') to return a str object. The str() function returns
the string.
Conversion of Sequence Types
List, Tuple and String are Python’s sequence types. They are ordered or
indexed collection of items.
A string and tuple can be converted into a list object by using the list()
function. Similarly, the tuple() function converts a string or list to a
tuple.
Types of Operators
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Membership Operators
• Identity Operators
Arithmetic Operators
• Addition Operator (+)
• Subtraction Operator (-)
• Multiplication Operator (*)
• Division Operator (/)
• Modulus Operator (%)
• Exponent Operator (**)
• Floor Division Operator (//)
Assignment Operators
• Augmented Addition Operator (+=)
• Augmented Subtraction Operator (-=)
• Augmented Multiplication Operator (*=)
• Augmented Division Operator (/=)
• Augmented Modulus Operator (%=)
• Augmented Exponent Operator (**=)
• Augmented Floor division Operator (//=)
Comparison Operators

< Less than a<b


> Greater than a>b
<= Less than or equal to a<=b
>= Greater than or equal to a>=b
== Is equal to a==b
!= Is not equal to a!=b
Logical Operators
and Returns True if both statements are true x < 5 and x < 10
Or Returns True if one of the statements is x < 5 or x < 4
true
Not Reverse the result, returns False if the not(x < 5 and x <
result is true 10)
Membership Operators
in Returns True if a sequence with the specified value X in Y
is present in the object
not in Returns True if a sequence with the specified value X not in Y
is not present in the object
Identity Operators
is Returns True if both variables are the same object X is Y
is not Returns True if both variables are not the same X is not Y
object
Operator Precedence
() Parentheses
** Exponentiation
Multiplication, division, floor division, and
* / // %
modulus
+ - Addition and subtraction
== != > >= < <= is is not in not Comparisons, identity, and membership
in operators
not Logical NOT
And AND
or OR
Python - Comments
There are three types of comments available in Python
• Single line Comments

• Multiline Comments

• Docstring Comments
Python - User Input
Every computer application should have a provision to accept data
from the user when it is running. This makes the application interactive.
Depending on how it is developed, an application may accept the user
input in the form of text entered in the console (sys.stdin), a graphical
layout, or a web-based interface.

You might also like