SlideShare a Scribd company logo
1
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
2
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
What is Python?
 Welcome to Python!
Python is a high-level programming language, with applications in numerous
areas, including web programming, scripting, scientific computing, and artificial
intelligence.
It is very popular and used by organizations such as Google, NASA, the CIA and
Disney.
Python is processed at runtime by the interpreter. There is no need to compile
your program before executing it.
Interpreter: A program that runs scripts written in an interpreted language such as Python.
The three major version of Python are 1.x, 2.x and 3.x. These are subdivided
into minor versions, such as 2.7 and 3.3.
Backwards compatible changes are only made between major versions; code
written in Python 3.x is guaranteed to work in all future versions. Both Python
Versions 2.x and 3.x are used currently.
Python has several different implementations, written in various languages.
The version CPython is used further.
Python source files have an extension of .py
3
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
The Python Console
 First Program:
On IDLE (Python GUI)
 The Python Console
Python console is a program that allows you to enter one line of python code,
repeatedly executes that line, and displays the output. This is known as REPL –
a read-eval-print loop.
To close a console, type in “quit()” or “exit()”, and press enter.
Ctrl-Z : sends an exit signal to the console program.
Ctrl-C : stops a running program, and is useful if you’ve accidentally created a
program that loops forever.
>>> print("Hello Universe")
Hello Universe
>>> quit()
4
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
Simple Operations
1. Simple Operations can be done on console directly.
* : Multiplication / : Division
2. Use of parentheses for determining which operations are performed
first.
3. Minus indicates negative number
4. Dividing by zero in Python produces an error, as no answer can be
calculated.
In Python, the last line of an error message indicates the error’s type. In
further examples only last line of error is written.
>>> 2 + 2
4
>>> 5 + 4 – 3
6
>>> 2 * (3 + 4)
14
>>> (-7 + 2) * (-4)
20
>>> 11/0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
11/0
ZeroDivisionError: integer division or modulo by zero
5
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
Floats
Floats are used in Python to represent numbers that aren’t integers. Extra
zeros at the number’s end are ignored.
A float can be added to an integer, because Python silently converts the
integer to float. This conversion is the exception rather the rule in Python –
usually you have to convert values manually if you want to operate on them.
Other Numerical Operations
 Exponentiation
The raising of one number to the power of another. This operation is
performed using two asterisks.
 Quotient & Remainder
Floor division is done using two forward slashes. ( // )
The modulo operator is carried out with a percent symbol. ( % )
>>> 2**5
32
>>> 9** (1/2)
3.0
>>> 20 // 6
3
>>> 1.25 % 0.5
0.25
6
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
Strings
A String is created by entering text between two single or double quotation
marks.
Some characters can’t be included in string, they must be escaped by placing
backslash before them.
Manually writing “n” can be avoided with writing the whole string between
three sets of quotes.
Simple Input & Output
In Python, we can use print function can be used to process output.
To get input from user, we can use input function. Which prompts user for i/p.
Returns what user enters as a string (with contents automatically escaped).
>>> print(1 + 1)
2
>>> input("Enter here:")
Enter here:222
222
>>> """System: Please Enter Input
User: Enters Input
System: Gives Output."""
'System: Please Enter InputnUser: Enters
InputnSystem: Gives Output.'
7
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
String Operations
Strings in python can be added using Concatenation using “+” operator.
Strings also can be multiplied.
Strings can’t be multiplied by other strings.
Strings also can’t be multiplied by floats, even if the floats are whole numbers.
>>> “Hello ” + ‘Python’ (works with both types of
quotes)
Hello Python
>>> “2” + “2”
22
>>> "2" + 3
TypeError: cannot concatenate 'str' and 'int' objects
>>> "Sumit" * 3
'SumitSumitSumit'
>>> 4 * '2'
'2222'
>>> '27' * '54'
TypeError: can't multiply sequence by non-int of type 'str'
>>>"PythonIsFun" * 7.0
TypeError: can't multiply sequence by non-int of type 'float'
8
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
Type Conversion
You can’t add two strings containing numbers together to produce the integer
result. The solution is type conversion.
>>> int("2")+int("3")
5
>>> float(input("Enter a number:"))+float(
input("Enter Another Number:"))
Enter a number : 42
Enter Another Number : 55
97.0
9
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
Variables
A variable allows you to store a value by assigning it to a name, which can be
used to refer to the value later in the program.
To assign a variable, “=” is used.
Variables can be used to perform operations as we did with numbers and
strings as they store value throughout the program.
In Python, variables can be assigned multiple times with different data types as
per need.
For Variable names only characters that are allowed are letters, numbers and
underscores. Numbers are not allowed at the start. Spaces are not allowed in
variable name.
Invalid variable name generates error.
Python is case sensitive programming language. Thus, Number and number
are two different variable names in Python.
>>> x=9
>>> print(2*x)
18
>>> x=123
>>> print(x)
123
>>> x='String'
>>> print(x+'!')
String!
>>> 2ndNo = 22
SyntaxError: invalid syntax
10
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
Trying to reference a variable you haven’t assigned, causes error.
You can use the del statement to remove a variable, which means the
reference from the name to the value is deleted & trying to use the variable
causes an error.
Deleted variables can be reassigned to later as normal.
You can also take the value of the variable from user input.
>>> foo = 'string'
>>> foo
'string'
>>> bar
NameError: name 'bar' is not defined
>>> del foo
>>> foo
NameError: name 'foo' is not defined
>>> foo=input("Enter a number: ")
Enter a number: 24
>>> print foo
24
11
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
In-Place Operators
In-place operators allow you to write code like ’x = x + 3’ more concisely,
as ‘x +=3’. The same thing is possible with other operators such as - , * , / and
% as well.
These operators can be used on types other than number such as
strings.
Many languages have special operators such as ‘++’ as a shortcut for ‘x+=1’.
Python does not have these.
>>> x=25
>>> x+=30
>>> print(x)
55
>>> x = "Learn"
>>> print(x)
Learn
>>> x+= "Python"
>>> print(x)
LearnPython
12
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
Dissecting Programs
 Comments
Single Line Comments
Multi-line Comments
 The print() Function
Displays the string value inside the parentheses written in the program on the
screen.
You can also use this function to put a blank line on the screen; just call
print() with nothing in between the parentheses.
 The input() Function
Waits for the user to type some text on the keyboard and press ENTER.
This function call evaluates to a string equal to the user’s text, and the previous
line of code assigns the myName variable to this string value.
You can think of the input() function call as an expression that evaluates to
whatever string the user typed in.
#This Single Line Comment
print(‘Hello World!’)
'''
This is another type of comment
This is multi-line comment.
'''
myName = input()
13
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
 The len() Function
You can pass the len() function a string value (or a variable containing a string),
and the function evaluates to the integer value of the number of characters in
that string.
Example:
 The str(), int() and float() Functions
If you want to concatenate an integer such as 21 with a string to pass to
print(), you’ll need to get the value '21', which is the string form of 21. The str()
function can be passed an integer value and will evaluate to a string value
version of it, as follows:
The str(), int(), and float() functions will evaluate to the string, integer, and
floating-point forms of the value you pass, respectively.
>>> str(21)
'21'
>>> print('I am ' + str(21) + ' years old.')
I am 21 years old
>>> len('hello')
5
>>> str(0)
'0'
>>> str(-3.14)
'-3.14'
14
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
TEXT AND NUMBER EQUIVALENCE
Although the string value of a number is considered a completely different
value from the integer or floating-point version, an integer can be equal to
a floating point .
>>> 42 == '42'
False
>>> 42 == 42.0
True
>>> 42.0 == 0042.000
True
Python makes this distinction because strings are text, while integers and
floats are both numbers .
>>> float('3.14')
3.14
>>> float(10)
10.0
>>> int('42')
42
>>> int('-99')
-99
>>> int(1.25)
1
>>> int(1.99)
1
15
BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
You were reading:
1. Basic Concepts In Python
 What is Python?
 The Python Console
 Simple Operations
 Floats
 Other Numerical Operations
 Strings
 Simple Input & Output
 String Operations
 Type conversions
 Variables
 In-Place Operators
 Dissecting Programs
2. Control Structures In Python
3. Functions & Modules In Python
4. Exceptions & Files In Python
5. More Types In Python
6. Functional Programming with Python
7. Object-Oriented Programming with Python
8. Regular Expressions In Python
9. Pythonicness & Packaging

More Related Content

What's hot

Python Presentation
Python PresentationPython Presentation
Python Presentation
Narendra Sisodiya
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
Sujith Kumar
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
ismailmrribi
 
Python Basics
Python BasicsPython Basics
Python Basics
tusharpanda88
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Python final ppt
Python final pptPython final ppt
Python final ppt
Ripal Ranpara
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Python programming
Python  programmingPython  programming
Python programming
Ashwin Kumar Ramasamy
 
Python basics
Python basicsPython basics
Python basics
Hoang Nguyen
 
Python Basics
Python BasicsPython Basics
Python Basics
primeteacher32
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
Priyanka Pradhan
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
Dipankar Achinta
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Variables in python
Variables in pythonVariables in python
Variables in python
Jaya Kumari
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
St. Petersburg College
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ayshwarya Baburam
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 

What's hot (20)

Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Python programming
Python  programmingPython  programming
Python programming
 
Python basics
Python basicsPython basics
Python basics
 
Python Basics
Python BasicsPython Basics
Python Basics
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 

Similar to Basic Concepts in Python

lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
Girish Khanzode
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
CBJWorld
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
HarishParthasarathy4
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
Kavitha713564
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
natnaelmamuye
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
jaggernaoma
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Mohd Sajjad
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
PPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptxPPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptx
tcsonline1222
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
Mukul Kirti Verma
 
chapter_5_ppt_em_220247.pptx
chapter_5_ppt_em_220247.pptxchapter_5_ppt_em_220247.pptx
chapter_5_ppt_em_220247.pptx
RAJAMURUGANAMECAPCSE
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
Mohammed Sikander
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
eteaching
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
ExcellenceAcadmy
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
ExcellenceAcadmy
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
MaheshGour5
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
IruolagbePius
 

Similar to Basic Concepts in Python (20)

lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
PPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptxPPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptx
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
chapter_5_ppt_em_220247.pptx
chapter_5_ppt_em_220247.pptxchapter_5_ppt_em_220247.pptx
chapter_5_ppt_em_220247.pptx
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 

Recently uploaded

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 

Basic Concepts in Python

  • 1. 1 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM
  • 2. 2 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM What is Python?  Welcome to Python! Python is a high-level programming language, with applications in numerous areas, including web programming, scripting, scientific computing, and artificial intelligence. It is very popular and used by organizations such as Google, NASA, the CIA and Disney. Python is processed at runtime by the interpreter. There is no need to compile your program before executing it. Interpreter: A program that runs scripts written in an interpreted language such as Python. The three major version of Python are 1.x, 2.x and 3.x. These are subdivided into minor versions, such as 2.7 and 3.3. Backwards compatible changes are only made between major versions; code written in Python 3.x is guaranteed to work in all future versions. Both Python Versions 2.x and 3.x are used currently. Python has several different implementations, written in various languages. The version CPython is used further. Python source files have an extension of .py
  • 3. 3 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM The Python Console  First Program: On IDLE (Python GUI)  The Python Console Python console is a program that allows you to enter one line of python code, repeatedly executes that line, and displays the output. This is known as REPL – a read-eval-print loop. To close a console, type in “quit()” or “exit()”, and press enter. Ctrl-Z : sends an exit signal to the console program. Ctrl-C : stops a running program, and is useful if you’ve accidentally created a program that loops forever. >>> print("Hello Universe") Hello Universe >>> quit()
  • 4. 4 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM Simple Operations 1. Simple Operations can be done on console directly. * : Multiplication / : Division 2. Use of parentheses for determining which operations are performed first. 3. Minus indicates negative number 4. Dividing by zero in Python produces an error, as no answer can be calculated. In Python, the last line of an error message indicates the error’s type. In further examples only last line of error is written. >>> 2 + 2 4 >>> 5 + 4 – 3 6 >>> 2 * (3 + 4) 14 >>> (-7 + 2) * (-4) 20 >>> 11/0 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> 11/0 ZeroDivisionError: integer division or modulo by zero
  • 5. 5 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM Floats Floats are used in Python to represent numbers that aren’t integers. Extra zeros at the number’s end are ignored. A float can be added to an integer, because Python silently converts the integer to float. This conversion is the exception rather the rule in Python – usually you have to convert values manually if you want to operate on them. Other Numerical Operations  Exponentiation The raising of one number to the power of another. This operation is performed using two asterisks.  Quotient & Remainder Floor division is done using two forward slashes. ( // ) The modulo operator is carried out with a percent symbol. ( % ) >>> 2**5 32 >>> 9** (1/2) 3.0 >>> 20 // 6 3 >>> 1.25 % 0.5 0.25
  • 6. 6 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM Strings A String is created by entering text between two single or double quotation marks. Some characters can’t be included in string, they must be escaped by placing backslash before them. Manually writing “n” can be avoided with writing the whole string between three sets of quotes. Simple Input & Output In Python, we can use print function can be used to process output. To get input from user, we can use input function. Which prompts user for i/p. Returns what user enters as a string (with contents automatically escaped). >>> print(1 + 1) 2 >>> input("Enter here:") Enter here:222 222 >>> """System: Please Enter Input User: Enters Input System: Gives Output.""" 'System: Please Enter InputnUser: Enters InputnSystem: Gives Output.'
  • 7. 7 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM String Operations Strings in python can be added using Concatenation using “+” operator. Strings also can be multiplied. Strings can’t be multiplied by other strings. Strings also can’t be multiplied by floats, even if the floats are whole numbers. >>> “Hello ” + ‘Python’ (works with both types of quotes) Hello Python >>> “2” + “2” 22 >>> "2" + 3 TypeError: cannot concatenate 'str' and 'int' objects >>> "Sumit" * 3 'SumitSumitSumit' >>> 4 * '2' '2222' >>> '27' * '54' TypeError: can't multiply sequence by non-int of type 'str' >>>"PythonIsFun" * 7.0 TypeError: can't multiply sequence by non-int of type 'float'
  • 8. 8 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM Type Conversion You can’t add two strings containing numbers together to produce the integer result. The solution is type conversion. >>> int("2")+int("3") 5 >>> float(input("Enter a number:"))+float( input("Enter Another Number:")) Enter a number : 42 Enter Another Number : 55 97.0
  • 9. 9 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM Variables A variable allows you to store a value by assigning it to a name, which can be used to refer to the value later in the program. To assign a variable, “=” is used. Variables can be used to perform operations as we did with numbers and strings as they store value throughout the program. In Python, variables can be assigned multiple times with different data types as per need. For Variable names only characters that are allowed are letters, numbers and underscores. Numbers are not allowed at the start. Spaces are not allowed in variable name. Invalid variable name generates error. Python is case sensitive programming language. Thus, Number and number are two different variable names in Python. >>> x=9 >>> print(2*x) 18 >>> x=123 >>> print(x) 123 >>> x='String' >>> print(x+'!') String! >>> 2ndNo = 22 SyntaxError: invalid syntax
  • 10. 10 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM Trying to reference a variable you haven’t assigned, causes error. You can use the del statement to remove a variable, which means the reference from the name to the value is deleted & trying to use the variable causes an error. Deleted variables can be reassigned to later as normal. You can also take the value of the variable from user input. >>> foo = 'string' >>> foo 'string' >>> bar NameError: name 'bar' is not defined >>> del foo >>> foo NameError: name 'foo' is not defined >>> foo=input("Enter a number: ") Enter a number: 24 >>> print foo 24
  • 11. 11 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM In-Place Operators In-place operators allow you to write code like ’x = x + 3’ more concisely, as ‘x +=3’. The same thing is possible with other operators such as - , * , / and % as well. These operators can be used on types other than number such as strings. Many languages have special operators such as ‘++’ as a shortcut for ‘x+=1’. Python does not have these. >>> x=25 >>> x+=30 >>> print(x) 55 >>> x = "Learn" >>> print(x) Learn >>> x+= "Python" >>> print(x) LearnPython
  • 12. 12 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM Dissecting Programs  Comments Single Line Comments Multi-line Comments  The print() Function Displays the string value inside the parentheses written in the program on the screen. You can also use this function to put a blank line on the screen; just call print() with nothing in between the parentheses.  The input() Function Waits for the user to type some text on the keyboard and press ENTER. This function call evaluates to a string equal to the user’s text, and the previous line of code assigns the myName variable to this string value. You can think of the input() function call as an expression that evaluates to whatever string the user typed in. #This Single Line Comment print(‘Hello World!’) ''' This is another type of comment This is multi-line comment. ''' myName = input()
  • 13. 13 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM  The len() Function You can pass the len() function a string value (or a variable containing a string), and the function evaluates to the integer value of the number of characters in that string. Example:  The str(), int() and float() Functions If you want to concatenate an integer such as 21 with a string to pass to print(), you’ll need to get the value '21', which is the string form of 21. The str() function can be passed an integer value and will evaluate to a string value version of it, as follows: The str(), int(), and float() functions will evaluate to the string, integer, and floating-point forms of the value you pass, respectively. >>> str(21) '21' >>> print('I am ' + str(21) + ' years old.') I am 21 years old >>> len('hello') 5 >>> str(0) '0' >>> str(-3.14) '-3.14'
  • 14. 14 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM TEXT AND NUMBER EQUIVALENCE Although the string value of a number is considered a completely different value from the integer or floating-point version, an integer can be equal to a floating point . >>> 42 == '42' False >>> 42 == 42.0 True >>> 42.0 == 0042.000 True Python makes this distinction because strings are text, while integers and floats are both numbers . >>> float('3.14') 3.14 >>> float(10) 10.0 >>> int('42') 42 >>> int('-99') -99 >>> int(1.25) 1 >>> int(1.99) 1
  • 15. 15 BASIC CONCEPTS IN PYTHON SUMIT S. SATAM You were reading: 1. Basic Concepts In Python  What is Python?  The Python Console  Simple Operations  Floats  Other Numerical Operations  Strings  Simple Input & Output  String Operations  Type conversions  Variables  In-Place Operators  Dissecting Programs 2. Control Structures In Python 3. Functions & Modules In Python 4. Exceptions & Files In Python 5. More Types In Python 6. Functional Programming with Python 7. Object-Oriented Programming with Python 8. Regular Expressions In Python 9. Pythonicness & Packaging