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

python unit1

python basic notes

Uploaded by

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

python unit1

python basic notes

Uploaded by

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

Contents

Programming Basics and Decision Making


1. Introduction:
Key features and applications of Python,
Python Editors and Compilers (Interpreters),
Using different offline and online Python IDE,
Interacting with Python programs

2. Data types:
Numeric, Boolean, Strings, Lists, Tuples, Sets, Dictionary

3. Variables:
Declaration and initialization

4. Simple Statements:
Taking inputs from user, Displaying outputs

5. Other concepts:
Operators, Expressions, Indentation, Comments, Casting

6. Conditional statements:
if…elif…else
Side 1
1. Introduction

Side 2
1. Introduction

Key features

• It is most widely used, high-level, general-purpose, multi-purpose and interpreted programming language.
• There are two major Python versions: Python 2 and Python 3 (latest version is 3.7.1, we can simply call it as
Python 3).
• It was created by Guido Van Rossum, and released in 1991. Python 3.0 was released in 2008.
• It is developed under an OSI-approved open source license, making it freely usable and distributable, even for
commercial use.
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). It is portable across operating
systems.
• Python has a simple syntax similar to the English language.
• Python runs on an interpreter system, meaning that code can be executed as soon as it is written.
• Python can be treated in a procedural way, an object-orientated way or a functional way.
• Its design philosophy emphasizes code readability.
• Its syntax allows programmers to express concepts in fewer lines of code.
• The best way we learn anything is by practice and exercise questions.
• Python uses new lines to complete a command instead of semicolons or parentheses.
• Python uses indentation through whitespace instead of curly-brackets, to define the scope of loops, functions and
classes.
• Python can connect to database systems. It can also read and modify files.
• It is very well suited for beginners and also for experienced programmers with other programming languages
like C++ and Java.
• The biggest strength of Python is huge collection of standard libraries that can be required in many
applications (NumPy for numerical calculations, Pandas for data analytics etc).
• you can download it for free from the following website: https://www.python.org/
Side 3
1. Introduction

Applications of Python

• Python can be used on a server to create web applications.


• It can be used to create GUI based desktop applications(Games, Scientific and Business
Applications).
• It is also used to create test frameworks and multimedia applications.
• It is used to develop operating systems and programming language.
• It can be used to handle image processing, text processing and natural language processing.
• It can be used to create programs for machine learning, deep learning, data science, big
data and data analytics applications.
• It can also perform complex mathematics along with all cutting edge technology in software
industry.

Organizations and tech-giant companies using Python :


1)Google(Components of Google spider and Search Engine)
2)Yahoo(Maps)
3)YouTube
4)Mozilla
5)Dropbox
6)Microsoft
7)Cisco
8)Spotify
9)Quora
10)Amazon
11)Uber etc.

Side 4
1. Introduction

Python Editors and Interpreters

• Python is interpreted language i.e it’s not compiled and the


interpreter will
check the code line by line.
• Code can be written in a text editor with .py extension and then it can
be put into the
python interpreter for execution.

Side 5
1. Introduction

Using different offline and online Python IDE

Offline IDE: Thonny, Pycharm, Spyder, IDLE, Netbeans or Eclipse


Online IDE: PyDev etc. https://www.w3schools.com/python/
https://www.w3resource.com/python/python-
tutorial.php
https://www.geeksforgeeks.org/python-
programming-language/

https://www.tutorialspoint.com/execute_python_onli
ne.php

Side 6
2. Datatypes

Side 7
2. Data types
Python is dynamically typed language(No need to mention data type based on value assigned, it
takes data type)

You can get the data type of any object by using the type( ) function

Side 8
2. Data types
Numeric Type: int, float, complex

Side 9
2. Data types
Boolean Type: bool

Side 10
2. Data types
Text Type (Strings): str

We can assign a string to a variable as below:


a = "Hello“

We can assign a multiline string to a variable by using three single or three


double quotes
as below:
(Note: in the result, the line breaks are inserted at the same position as in the
code.): a = ’‘’Hi everyone,
This is Python Class.’‘’
a = ”””Hi everyone,
This is Python Class.”””

To concatenate, or combine, two strings we can use the + operator as below:


a = "Hello"
b = “Class"
c=a+""+b

We cannot combine strings and numbers like this:


age = 30
txt = "My name is XYZ, I am " + age
Side 11
2. Data types
Text Type (Strings): str …continued

Side 12
2. Data types
Text Type (Strings): str …continued

Side 13
2. Data types
Sequence Type: list
List is a collection which is ordered, indexed and changeable. It allows
duplicate
members.
Lists are written with square brackets [ ].
We can access list items by referring to the index number, inside square
brackets.

Side 14
2. Data types
Sequence Type: list …continued

Side 15
2. Data types
Sequence Type: tuple
Tuple is a collection which is ordered, indexed and unchangeable.
It allows duplicate members.
Tuples are written with round brackets ( ).
We can access tuple items by referring to the index number,
inside square brackets.

Side 16
2. Data types
Sequence Type: tuple …continued

Side 17
2. Data types
Set Type: set
Set is a collection which is unordered, unindexed and
unchangeable. It does not allow duplicate members.
Sets are written with curly brackets { }.

Side 18
2. Data types
Mapping Type (Dictionary): dict
Dictionary is a collection which is unordered, indexed and
changeable. It does not allow duplicate members.
Dictionaries are written with curly brackets, and they have keys and
values. We can access the items of a dictionary by referring to its key
name, inside square brackets.

Side 19
3. Variables

Side 20
3. Varia ble s
Decla rat ion and init ializ atio n

Varia bles are co nta iner s fo r sto ring da ta


va lue s. P yth on h as no co mm an d fo r
de cla ring a v ari able.
A var iabl e is cre ate d the moment you fir st a ssign a val ue t o it .

Rules for Py tho n va ria bles :


❖ A var iabl e na me mu st s tart with a l ette r or the und ersc ore cha rac ter
❖ A var iabl e na me can not sta rt w ith a nu mbe r
❖ A var iabl e na me can on ly c onta in a lph a-nu me ric c har acte rs a nd
under scores (A-z, 0-9,
and _ )
❖ V aria ble names are cas e-se nsi tive (ag e, A ge and AGE a re t hree dif fere nt
variables )

Variables do not need to be dec lared with any par ticular t ype and
ca n e ven cha nge type a fter the y ha ve bee n se t.
String va riab les can be dec lare d ei ther by usin g si ngle

or double quotes . We c an a ssi gn v alues t o multi ple

va ria bles in one line .

W e c an a ssi gn t he s am e v alue to mu ltipl e va ria bles in one line .


Side 21
3. Variables
Declaration and initialization …continued

Side 22
4. Simple Statements

Side 23
4. Simple Statements
Taking inputs from user

Python 3.6 uses the input( ) method while Python 2.7 uses the
raw_input( ) method.

Side 24
4. Simple Statements
Displaying outputs

print statement is often used to output variables.


To concatenate two or more strings or string variables the +
character is used. For numbers, the + character works as a
mathematical operator.
If you try to combine a string and a number, Python will give
you an error.

Side 25
5. Other Concepts

Side 26
5. Other Concepts
Operators and Expressions

• Arithmetic operators (+, -, *, /, %, **, //)


• Assignment operators (=, +=, -=, *=, /=, %=, **=, //=, &=, |=,
<<=, >>=, ^=)
• Comparison operators (==, !=, <, >, <=, >=)
• Logical operators (and, or, not)
• Identity operators (is, is not)
• Membership operators (in, not in)
• Bitwise operators (&, |, ^, ~, <<, >>)

Side 27
5. Other Concepts
Operators and Expressions …continued

Arithmetic operators
** is used for Exponentiation and // is used for
Floor division.

Side 28
5. Other C oncep ts

Operators and Expressions …continued

L og i cal op er ato rs

S id e
29
5. Other Concepts
Operators and Expressions …continued

Identity operators

Side 30
5. Other Concepts
Operators and Expressions …continued

Identity operators

Side 31
5. Other Concepts
Operators and Expressions …continued

Membership operators

Side 32
5. Other Concepts
Indentation

Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.

Python uses indentation to indicate a block of code. Other languages often use curly-
brackets for this purpose.

Python will give you an error if you skip the indentation.

The number of spaces is up to you as a programmer, but it has to be at least one.

You have to use same number of spaces in the same block of code, otherwise Python
will give you an error.

Correct Example:
if 5 > 2:
print("Five is greater than
two!") if 5 > 2:
print("Five is greater than two!")

Side 33
5. Other Concepts
Comments

Comments starts with a #, and Python will ignore them.

Python does not really have a syntax for multi line

comments. To add a multiline comment you could

insert a # for each line.

Since Python will ignore string literals that are not assigned to a
variable, you can add a multiline string (triple quotes) in your
code, and place your comment inside it.

Side 34
5. Other Concepts
Casting

Casting can be done using constructors : int( ), float( ), str( )

Side 35
6. Conditional Statements

Side 36
6. Conditional Statements
if…elif…else

a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

If you have only one statement to execute, you can put it on the same line as the if
statement.

if a > b: print("a is greater than b")

Side 37
6. Conditional Statements
if…elif…else

Side 38
6. Conditional Statements
if…elif…else

Ternary Operators, or Conditional Expressions


If you have only one statement to execute, one for if, and one for else, you
can put it all on the same line.
a=2
b = 330
print("A") if a > b else print("B")

You can also have multiple else statements on the same line.
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")

Logical operators to combine conditional statements


The and/or keywords are logical operators, and can be used as below:

if a > b and a > c:

if a > b or a > c:
6. Conditional Statements
Ternary operator
6. Conditional Statements
Logical operator

Side 41
6. Conditional Statements
if…elif…else

Nested if

You can have if statements inside if statements, this is


called nested if statements.

x = 41

if x > 10:
print("Above
ten,") if x > 20:
print("and also above
20!") else:
print("but not above 20.")

Side 42
6. Conditional Statements
Nested if

Side 43
6. Conditional Statements
if…elif…else

Pass statement
if statements cannot be empty, but if you for some reason have an if statement
with no content, put in the pass statement to avoid getting an error.

a = 33
b = 200
if b > a:
pass

Side 44
6. Conditional Statements
Pass statement

Side 45
Queries ?

Side 46

You might also like