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

Python Ch1

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

Python Ch1

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

CHAPTER 1

Python
Introduction

Ashika Patel Dept. of Computer Engineering LJ


Python Programming: Introduction
What We Give You?
•What is Python?
•Difference between
Compiling and interpreting
language.
•History of Python
•Python Features and
Advantages
•Why do people use
Python?
Ashika Patel •Install
Dept. of Python
Computer IDE.
Engineering LJ
Introduction
What is Python?

• Python is a general purpose language that


is often applied in a scripting roles.
• So, python is programming language as well
as scripting language.
• Python is also called an Interpreter
language.
An interpreted language is a
programming language which are generally
interpreted, without compiling a program into
machine instructions.
Example:- JavaScript, Perl, PHP, Python, etc…
Ashika Patel Dept. of Computer Engineering LJ
Introduction
History of Python
 The implementation of
Python was started in
December 1989 by Guido
Van Rossum in Netherland.
In February
1991, he published the
labeled version 0.9.0.
 In 1994, Python 1.0 was
released with new features
 In 2000, Python 2.0 was
released with new features
Ashika Patel Dept.
Onof Computer
December 3,
Engineering 2008,
LJ
Introduction
Compiling and interpreting language
• Many languages require you to compile
(translate) your program into a form that the
compile execute
machine understands.
source code byte code output
Hello.java Hello.class

• Python is instead directly


interpret interpreted into
machine instructions.
source code output
Hello.py

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Python Features and Advantages:
1. Easy to Code and Easy to Read
2. Free and Open-Source
3. Robust Standard Library
4. Interpreted
5. Portable
6. Object-Oriented and Procedure-
Oriented
7. Extensible
8. Expressive Language
9. Support for GUI
10.Dynamically Typed
11.High-level Language
Ashika Patel Dept. of Computer Engineering LJ
Introduction
Why do people use Python?
Python is commonly used for developing
websites and software, task automation,
data analysis, and data visualization.

What can we do with Python?


1) Data analysis and machine
learning
2) Web development
3) Automation or scripting
4) Software testing and prototyping
5) Everyday tasks – like Keep track of
stock market, Update your grocery
Ashika Patel Dept. of Computer Engineering LJ
Introduction
Installing Python IDE.

Step 1 − Select Version of Python to


Install
Step 2 − Download Python Executable
Installer
Step 3 − Run Executable Installer
Step 4 − Verify Python is installed on
Windows
Step 5 − Verify Pip was installed

Ashika Patel Dept. of Computer Engineering LJ


Introduction
A Sample Code on IDLE

>>> print('Hello, >>> type('Hello,


World!') World!')
Hello, World! <class 'str'>
>>> print(1) >>> type(1)
1 <class 'int'>
>>> print(2.2) >>> type(2.2)
2.2 <class 'float'>
>>> >>>
print('Abc@123') print(''Abc@123'')
Abc@123 <class 'str'>

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Python Program Execution
List out Different ways to run Python
Script
Here are the ways with which we
can run a Python script.
• Interactive Mode – We have to write
code in command prompt line by
line.
• Command Line – We have to make
python file first and save by .py
extension. This file you can run in
command prompt by write python
first and then your filename.
Ashika Patel Dept. of Computer Engineering LJ
Introduction
Python Variables……
Rules for creating variables in Python:
• A variable name must start with a letter
or the underscore character.
• A variable name cannot start with a
number.
• A variable name can only contain alpha-
numeric characters and underscores (A-z,
0-9, and _).
• Variable names are case-sensitive (name,
Name and NAME are three different
variables).
• The reserved words (keywords) cannot be
Ashika Patel Dept. of Computer Engineering LJ
Introduction
Something New about Python Variable….
1. Python has no command for declaring a
variable. We just have to write variable name
and assign the value in it.
2.A variable is created the moment you first
assign a value to it.
3.Variables do not need to be declared with any
particular type, and can even change type
after referring to a new value and type.
4.Python allows assigning a single value to
several variables simultaneously with “=”
operators.
5.they have been set. If we use the same name,
the
Ashika variable starts
Patel Dept. ofAssigning different values
Computer Engineering LJ
Introduction
Strings in Python…
String:
• Strings are arrays of bytes representing
Unicode characters.
• Python does not have a character data type, a
single character is simply a string with a length
of 1.
Example:-
• String Output:-
is not a small. It can also big as million
s = 'P'
characters.
print(s) P
type(s) <class ‘str’>
language = Python
'Python' <class ‘str’>
print(language)
type(language)
Ashika Patel Dept. of Computer Engineering LJ
Introduction
Strings in Python…
String:-
[0] [1] [2] [3] [4] [5]
P y t h o n

[-6] [-5] [-4] [-3] [-2] [-1]


Square brackets can be used y to access
language = 'Python' P
elements of the string.
letter = language[1]
print(letter) n
TypeError : string
print(language[0]) indices must be
print(language[-1]) integers
print(language[1.5])
Ashika Patel Dept. of Computer Engineering LJ
Introduction
Strings Slicing
• A segment of a string is called a slice.
Selecting a slice is similar to selecting a
character:
• We can specify start, stop and step
(optional) within the square brackets as:
string[start:stop:step]
start: It is the index from where the slice starts.
The default value is 0.
stop: It is the index at which the slice stops.
The character at this index is not
included in the slice. The default value is
the length of the string.
Ashika Patel Dept. of Computer Engineering LJ
Introduction
String Slicing Example…
s = 'Welcome to
LJKU' Output:
print(s[0:8])
print(s[11:16]) Welcome
LJKU
print(s[:]) Welcome to LJKU
print(s[0:]) Welcome to LJKU
print(s[3:3])
UKJL ot emocleW
print(s[ : :-1])
Wloet JU
print(s[0: :2]) UJ teolW
print(s[
Ashika Patel : :-2]) Dept. of Computer Engineering LJ
Introduction
String Method:
1. capitalize( )
2. lower( )
3. upper()
4. title()
5. swapcase()
6. count(<sub>, <start>, <end>)
7. find(<sub>, <start>, <end>)
8. lstrip()
9. rstrip()
10.strip()
11.join(<iterable>)
12.split(sep=None, maxsplit=-1)
Ashika Patel Dept. of Computer Engineering LJ
To fix this problem, use the escape character \"

Introduction
Escape Characters:
Escape Characters
To insert characters that are illegal in a
string, use an escape character. An escape
character is a backslash (\) followed by the
character
>>>syou= “we want
aretothe
insert.
so-called ''Vikings''
from the north”
SyntaxError: invalid syntax
To fix this problem, use the escape
>>>s =
character \" ''we are the so-called \''Vikings\''
from the north''
'we are the so-called ''Vikings'' from the
north'

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Type Conversion:
You can convert from one type to another with
the int(), float(), and complex() methods
x = 1 # int Output:
y = 2.8 # float
z = 1j # complex 1.0
a = float(x) #convert from int <class ‘float’>
to float 2
b = int(y) #convert from float <class 'int'>
to int (1+0j)
c = complex(x) #convert <class
from int to complex ‘complex'>
print(a) print(type(a))
Ashika Patel Dept. of Computer Engineering LJ
Introduction
Operators:
Operators in general are used to perform
operations on values and variables. These are
standard symbols used for the purpose of
logical and arithmetic operations.

Types of Operators:
1. Arithmetic Operators
2. Comparison/Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Identity Operators
7. Membership
Ashika Patel Operators
Dept. of Computer Engineering LJ
Introduction
Arithmetic Operators:

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Comparison / Relational Operators:

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Logical Operators:

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Bitwise Operators:

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Assignment Operators:

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Identity Operators:

Ashika Patel Dept. of Computer Engineering LJ


Introduction
Membership Operators:

Ashika Patel Dept. of Computer Engineering LJ

You might also like