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

2 Python

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

2 Python

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

Python

Mr.M.Yaseen Pasha ,
Assistant Professor,
CVR College Of Engineering,
How to Install & Write
Python Code?
Installation

https://www.python.org/downloads/
How to Run Python Code ?
Now, What is Code
Environment ?
There are 3 main types of environments:

Text Editors
Full IDEs
Notebook Environments
Text Editors

• General editors for any text file

• Work with a variety of file types

• Can be customized with plugins and add-ons

Most popular: Visual Studio Code & Atom


● Full IDEs

○ Development Environments designed specifically for Python.

○ Larger programs & Only community editions are free.

○ Designed specifically for Python, lots of extra functionality.

Most popular: PyCharm and Spyder


https://www.jetbrains.com/pycharm/download/
● Notebook Environments

○ Great for learning.

○ See input and output next to each other.

○ Support in-line markdown notes, visualizations, videos, and


more.

○ Special file formats that are not .py

Most popular is Jupyter Notebook


We will install free
Anaconda distribution.
This distribution includes Python
as well as many other useful
libraries, including Jupyter.
Anaconda can easily be installed on to any OS,

Windows, MacOS, or Linux.


Download Anaconda from -

www.anaconda.com/distribution/

3.8 Version
What & Why are

Objects & Data Structures ?


- 12

- 34.6

- “Python FDP”
Your basic building bricks when constructing huge
Program
Name Integers
Type int

Explanation Whole numbers, such as: 3 300 200


Name Floating point
Type Float

Explanation Numbers with a decimal point: 2.3 4.6


100.0
Name String
Type str

Explanation Ordered sequence of characters: "hello"


'Sammy' "2000" "வணக்கம்"
Name List
Type list

Explanation Ordered sequence of objects:


[10,"hello",200.3]
Name Dictionaries
Type dict

Explanation Unordered Key:Value pairs: {"mykey" :


"value" , "name" : "Frankie"}
Name Sets
Type set

Explanation Unordered collection of unique objects:


{"a","b"}
Name Tuples
Type tup

Explanation Ordered immutable sequence of objects:


(10,"hello",200.3)
Name Boolean
Type Bool

Explanation Logical value indicating True or False


We'll learn about the following topics:

Numbers in Python!
1.) Types of Numbers in Python
2.) Basic Arithmetic
3.) Differences between classic division
and floor division
4.) Object Assignment in Python
• Python has various "types" of numbers (numeric
literals). We'll mainly focus on integers and floating
point numbers.
• Integers are just whole numbers, positive or
negative. For example: 2 and -2 are examples of
Types of integers.
• Floating point numbers in Python are notable
numbers because they have a decimal point in them, or
use an exponential (e) to define the number.
For example 2.0 and -2.1 are examples of
floating point numbers. 4E2 (4 times 10 to the
power of 2) is also an example of a floating
point number in Python.
Rules for variable names
•names can not start with a number

Variable •names can not contain spaces, use _ intead


•names can not contain any of these symbols:

Assignmen
:'",<>/?|\!@#%^&*~-+
•it's considered best practice (PEP8) that names are lowercase with
underscores
•avoid using Python built-in keywords like list and str

t •avoid using the single characters l (lowercase letter el), O


(uppercase letter oh) and I (uppercase letter eye) as they can be
confused with 1 and 0
Pros of Dynamic Typing
•very easy to work with
•faster development time
Pros and
Cons of Cons of Dynamic Typing
•may result in unexpected bugs!
Dynamic •you need to be aware of type()
Typing
Print Statement
Strings
• Strings are used in Python to record text information, such as names. Strings in
Python are actually a sequence, which basically means Python keeps track of
every element in the string as a sequence. For example, Python understands the
string "hello' to be a sequence of letters in a specific order. This means we will be
able to use indexing to grab particular letters (like the first letter, or the last
letter).
• This idea of a sequence is an important one in Python and we will touch upon it
later on in the future.
In this lecture we'll learn about the following:
1.) Creating Strings
2.) Printing Strings
Strings 3.) String Indexing and Slicing
4.) String Properties
5.) String Methods
6.) Print Formatting
Lists

Earlier when discussing strings we introduced the concept of a sequence in Python. Lists
can be thought of the most general version of a sequence in Python. Unlike strings, they
are mutable, meaning the elements inside a list can be changed!
In this section we will learn about:
1.) Creating lists
2.) Indexing and Slicing Lists
3.) Basic List Methods
4.) Nesting Lists
5.) Introduction to List Comprehensions
Lists are constructed with brackets [] and commas separating every element in the list.
Let's go ahead and see how we can construct lists!
Assignment
• # 1. Create a list with certain values
• # 2. Append 10 to the created list - use append() function
• # 3. Delete the last value in the list - use pop() function
• # 4. Delete the 4th index in the list - use pop() function
• # 5. Add ['what', 'is', 'your', 'name'] to the list - use extend() function
• # 6. Access the 5th index in the list
• # 6. Access the last index in the list
• # 7. Create a list with multiple values and a) reverse it using REVERSE
function - use reverse() function, b) reverse it Without using the reverse
function
Dictionaries
We've been learning about sequences in Python but now we're going to switch gears and learn
about mappings in Python. If you're familiar with other languages you can think of these
Dictionaries as hash tables.
This section will serve as a brief introduction to dictionaries and consist of:
1.) Constructing a Dictionary
2.) Accessing objects from a dictionary
3.) Nesting Dictionaries
4.) Basic Dictionary Methods
So what are mappings? Mappings are a collection of objects that are stored by a key, unlike a
sequence that stored objects by their relative position. This is an important distinction, since
mappings won't retain order since they have objects defined by a key.
A Python dictionary consists of a key and then an associated value. That value can be almost any
Python object.
Assignment
• # 1. Create a dictionary with keys and values
• # 2. Add a new key with KEY NAME as 'new_key' and VALUE as 'This_is_ a_new_key'
• # 3. Delete 'new_key'
• # 4. Acccess all the keys in the created dictionary
• # 5. Access all the values in the created dictionary
• # 6. Create a empty dictionary as d
• # 7 .Create the following variables in the dictionary d
• # d['animal'] = ['Dog','cat']
• # d['animal1'] = 'Dog'
• # d['animal2'] = 'Dog','cat'
• # d['answer'] = 42
• # 8. Access the value 'Dog' in the key 'animal'
Tuples

In Python tuples are very similar to lists, however, unlike lists they are immutable meaning
they can not be changed. You would use tuples to present things that shouldn't be changed,
such as days of the week, or dates on a calendar.
In this section, we will get a brief overview of the following:
1.) Constructing Tuples
2.) Basic Tuple Methods
3.) Immutability
4.) When to Use Tuples
You'll have an intuition of how to use tuples based on what you've learned about lists. We
can treat them very similarly with the major distinction being that tuples are immutable.
Set and Booleans
Sets
Sets are an unordered collection of unique elements. We can construct
them by using the set() function. Let's go ahead and make a set to see
how it works

Booleans
Python comes with Booleans (with predefined True and False displays
that are basically just the integers 1 and 0). It also has a placeholder
object called None. Let's walk through a few quick examples of
Booleans (we will dive deeper into them later in this course).
Assignment
• # Questions on Tuple,set:
• # Tuple:
• # Create an empty tuple
• # Create a tuple with multiple data types
• # Access the first two elements of the tuple
• # Pop the last value
• # Create a tuple:(1,2,3,4,5,5,4,6,7,8,9,10). Count number of 5 in the tuple
• # set:
• # create an empty set as s
• # Add 1 to s, add 2 to s, add 1 to s
• # Create a list [1,2,3,4,5,5,4,6,7,8,9,10]
• # Return the set value of the above list as set1
Start with Files Handling ( Why, How and
What ?)
Why File Handling ?
- In general file handling is used for storing your files, or creating a new
file.

- Please post your answers where all file handling is used ?


How File Handling ?
There are five major operations that can be performed on a file are:
1. Creation of file
2. Opening an existing file
3. Reading data from a file
4. Writing data in a file
5. Closing a file
What is File Handling in Python ?
Python too supports file handling and allows users to handle files i.e., to read
and write files, along with many other file handling options, to operate on
files.
- syntax being : open(filename, mode)

- “ r “, for reading.
- “ w “, for writing.
- “ a “, for appending.
- “ r+ “, for both reading and writing
Create File
# Python code to create a file
file = open(‘fcs.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.close()
Read file
# Python code to illustrate read() mode
file = open("fcs.txt", "r")
print file.read()
file.close()
Append File
# Python code to illustrate append() mode
file = open(‘emp.txt','a')
file.write("This will add this line")
file.close()
Using with() function
# Python code to illustrate with()
with open("fcs.txt") as file:
data = file.read()
# do something with data
Split function with Python -
# Python code to illustrate split() function

with open("fcs.txt", "r") as file:


data = file.readlines()
for line in data:
word = line.split()
print (word)

You might also like