SlideShare a Scribd company logo
Python
Session 1– Introduction
Dr. Wessam M. Kollab
What is Python?
 Python is a general-purpose interpreted, interactive, object-oriented, and high-
level programming language. It was created by Guido van Rossum during
1985- 1990.
 It is used for:
• web development (server-side).
• software development.
• Mathematics.
• GUI applications.
• Scrape data from websites.
• Analyze Data.
• Game Development.
• Data Science
Why Python?
 Python is an interpreted language
when you run python program an interpreter will parse python program line by line basis, as
compared to compiled languages like C or C++, where compiler first compiles the program and
then start running.
interpreted languages are a little slow as compared to compiled languages.
 Python is Dynamically Typed
Python doesn't require you to define variable data type ahead of time. Python automatically
infers the data type of the variable based on the type of value it contains.
myvar = "Hello Python"
The above line of code assigns string "Hello Python" to the variable myvar, so the type
of myvar is string.
myvar = 1 Now myvar variable is of type int.
Why Python?
 Python is strongly typed
If you have programmed in PHP or javascript. You may have noticed that they both convert data of
one type to another automatically.
In JavaScript
1 + "2" will be ’12’
Here, before addition (+) is carried out, 1 will be converted to a string and concatenated to "2",
which results in '12', which is a string. However, In Python, such automatic conversions are not
allowed, so
1 + "2" will produce an error.
Why Python?
 Write less code and do more
Programs written in Python are usually 1/3 or 1/5 of the Java code. It means we can write
less code in Python to achieve the same thing as in Java.
To read a file in Python you only need 2 lines of code:
with ("myfile.txt") as f:
print(f.read())
 Python works on different platforms (Windows, Mac, Linux, etc).
Why Python?
 The fastest-growing major programming language, as you can see in
Figure 1-4.
Python Syntax compared to other
programming languages
 Python was designed for readability, and has some similarities to the
English language with influence from mathematics.
 Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
 Python relies on indentation, using whitespace, to define scope; such
as the scope of loops, functions and classes. Other programming
languages often use curly-brackets for this purpose.
Data: Types, Values, Variables, and Names
Python Data Are Objects
 Variables are nothing but reserved memory locations to store values. This
means that when you create a variable you reserve some space in memory.
 Based on the data type of a variable, the interpreter allocates memory and
decides what can be stored in the reserved memory. Therefore, by
assigning different data types to variables, you can store integers, decimals
or characters in these variables.
 Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space.
The declaration happens automatically when you assign a value to a
variable.
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
Data: Types, Values, Variables, and Names
Python variable names have some rules:
 They can contain only these characters:
 Lowercase letters (a through z)
 Uppercase letters (A through Z)
 Digits (0 through 9)
 Underscore (_)
 They are case-sensitive: thing, Thing, and THING are different names.
 They must begin with a letter or an underscore, not a digit.
 They cannot be one of Python’s reserved words (also known as keywords).
>>> help("keywords")
>>> import keyword
>>> keyword.kwlist
Data: Types, Values, Variables, and Names
Variables Are Names, Not Places
In Python, if you want to know the type of variable, you can use
type(variable Name)
>>> type(7)
<class 'int’>
Assigning to Multiple Names
>>> a = b = c = 2
>>> a = b = c = 1,2,”Ahmed”
Reassigning a Name
Because names point to objects, changing the value assigned to a name just makes the name
point to a new object.
Copying
As you saw in Figure 2-4, assigning an existing variable a to a new variable named b just
makes b point to the same object that a does.
Data: Types, Values, Variables, and
Names
In this code, Python did the following:
 Created an integer object with the value 5
 Made a variable y point to that 5 object
 Incremented the reference count of the object with value 5
 Created another integer object with the value 12
 Subtracted the value of the object that y points to (5) from the value 12 in the
(anonymous) object with that value
 Assigned this value (7) to a new (so far, unnamed) integer object
 Made the variable x point to this new object
 Incremented the reference count of this new object that x points to
Python Numbers
This data type supports only numerical values like 1, 31.4, -
1000, 0.000023, 88888888.
Python supports 3 different numerical types.
1. int - for integer values like 1, 100, 2255, -999999, 0, 12345678.
2. float - for floating-point values like 2.3, 3.14, 2.71, -11.0.
3. complex - for complex numbers like 3+2j, -2+2.3j, 10j, 4.5+3.14j.
Python operators
Augmented Assignment Operator
 These operator allows you write shortcut assignment statements. For e.g:
Bases
 You can go the other direction, converting an integer to a string with any of these
bases:
>>> value = 65
>>> bin(value)
'0b1000001'
>>> oct(value)
'0o101'
>>> hex(value)
'0x41’
 The chr() function converts an integer to its single character string equivalent:
>>> chr(65)
‘A’
 And ord() goes the other way:
>>> ord('A')
65
Number Type Conversion
 To change other Python data types to an integer, use the int() function.
>>> int(True)
1
>>> int(False)
0
 Turning this around, the bool() function returns the boolean equivalent of an integer:
>>> bool(1)
True
>>> bool(0)
False
>>> int(98.6)
98
>>> int(1.0e4)
10000
Number Type Conversion
 To getting the integer value from a text string>>>
int(True)
>>> int(‘99')
99
>>> int('-23')
-23
>>> int('+12')
12
>>> int('1_000_000')
1000000
 If the string represents a nondecimal
integer, you can include the base:
>>> int('10', 2) # binary
2
>>> int('10', 8) # octal
8
>>> int('10', 16) # hexadecimal
16
>>> int('10', 22) # chesterdigital
22
Number Type Conversion
 you can convert a string containing characters that
would be a valid float
>>> float('98.6')
98.6
>>> float('-1.5')
-1.5
>>> float('1.0e4')
10000.0
 When you mix integers and floats, Python
automatically promotes the integer values to float
values
>>> 43 + 2.
45.0
 Python also promotes booleans to
integers or floats:
>>> False + 0
0
>>> False + 0.
0.0
>>> True + 0
1
>>> True + 0.
1.0
Comment #
 You’ll usually see a comment on a line by itself, as shown here:
>>> # 60 sec/min * 60 min/hr * 24 hr/day
>>> seconds_per_day = 86400
 Or, on the same line as the code it’s commenting:
>>> seconds_per_day = 86400 # 60 sec/min * 60 min/hr * 24 hr/day
Continue Lines with 
>>> sum = 1 + 
... 2 + 
... 3 + 
... 4
>>> sum
10
------------------------------------------------------------------
----
>>> sum = 1 +
File "<stdin>", line 1
sum = 1 +
^
SyntaxError: invalid syntax
>>> sum = (
... 1 +
... 2 +
... 3 +
... 4)
>>>
>>> sum

More Related Content

Similar to python

Python numbers
Python numbersPython numbers
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
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
MaheshPandit16
 
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
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
sunilchute1
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
Akanksha Bali
 
GRADE 11 Chapter 5 - Python Fundamentals.pptx
GRADE 11 Chapter 5 - Python Fundamentals.pptxGRADE 11 Chapter 5 - Python Fundamentals.pptx
GRADE 11 Chapter 5 - Python Fundamentals.pptx
DeepaRavi21
 
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
manikamr074
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
natnaelmamuye
 
PYTHON.pdf
PYTHON.pdfPYTHON.pdf
Python programming
Python  programmingPython  programming
Python programming
Ashwin Kumar Ramasamy
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdf
Ida Lumintu
 
Python - variable types
Python - variable typesPython - variable types
Python - variable types
Learnbay Datascience
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
deepak teja
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
Aswin Krishnamoorthy
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of Python
Elewayte
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
TOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfTOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdf
EjazAlam23
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
Mr. Vikram Singh Slathia
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
Mukul Kirti Verma
 

Similar to python (20)

Python numbers
Python numbersPython numbers
Python numbers
 
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
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
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
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
 
GRADE 11 Chapter 5 - Python Fundamentals.pptx
GRADE 11 Chapter 5 - Python Fundamentals.pptxGRADE 11 Chapter 5 - Python Fundamentals.pptx
GRADE 11 Chapter 5 - Python Fundamentals.pptx
 
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
PYTHON.pdf
PYTHON.pdfPYTHON.pdf
PYTHON.pdf
 
Python programming
Python  programmingPython  programming
Python programming
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdf
 
Python - variable types
Python - variable typesPython - variable types
Python - variable types
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of Python
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
 
TOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfTOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdf
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 

Recently uploaded

The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

python

  • 2. What is Python?  Python is a general-purpose interpreted, interactive, object-oriented, and high- level programming language. It was created by Guido van Rossum during 1985- 1990.  It is used for: • web development (server-side). • software development. • Mathematics. • GUI applications. • Scrape data from websites. • Analyze Data. • Game Development. • Data Science
  • 3. Why Python?  Python is an interpreted language when you run python program an interpreter will parse python program line by line basis, as compared to compiled languages like C or C++, where compiler first compiles the program and then start running. interpreted languages are a little slow as compared to compiled languages.  Python is Dynamically Typed Python doesn't require you to define variable data type ahead of time. Python automatically infers the data type of the variable based on the type of value it contains. myvar = "Hello Python" The above line of code assigns string "Hello Python" to the variable myvar, so the type of myvar is string. myvar = 1 Now myvar variable is of type int.
  • 4. Why Python?  Python is strongly typed If you have programmed in PHP or javascript. You may have noticed that they both convert data of one type to another automatically. In JavaScript 1 + "2" will be ’12’ Here, before addition (+) is carried out, 1 will be converted to a string and concatenated to "2", which results in '12', which is a string. However, In Python, such automatic conversions are not allowed, so 1 + "2" will produce an error.
  • 5. Why Python?  Write less code and do more Programs written in Python are usually 1/3 or 1/5 of the Java code. It means we can write less code in Python to achieve the same thing as in Java. To read a file in Python you only need 2 lines of code: with ("myfile.txt") as f: print(f.read())  Python works on different platforms (Windows, Mac, Linux, etc).
  • 6. Why Python?  The fastest-growing major programming language, as you can see in Figure 1-4.
  • 7. Python Syntax compared to other programming languages  Python was designed for readability, and has some similarities to the English language with influence from mathematics.  Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.  Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.
  • 8. Data: Types, Values, Variables, and Names Python Data Are Objects  Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.  Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.  Assigning Values to Variables Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string
  • 9. Data: Types, Values, Variables, and Names Python variable names have some rules:  They can contain only these characters:  Lowercase letters (a through z)  Uppercase letters (A through Z)  Digits (0 through 9)  Underscore (_)  They are case-sensitive: thing, Thing, and THING are different names.  They must begin with a letter or an underscore, not a digit.  They cannot be one of Python’s reserved words (also known as keywords). >>> help("keywords") >>> import keyword >>> keyword.kwlist
  • 10. Data: Types, Values, Variables, and Names Variables Are Names, Not Places In Python, if you want to know the type of variable, you can use type(variable Name) >>> type(7) <class 'int’> Assigning to Multiple Names >>> a = b = c = 2 >>> a = b = c = 1,2,”Ahmed” Reassigning a Name Because names point to objects, changing the value assigned to a name just makes the name point to a new object. Copying As you saw in Figure 2-4, assigning an existing variable a to a new variable named b just makes b point to the same object that a does.
  • 11. Data: Types, Values, Variables, and Names In this code, Python did the following:  Created an integer object with the value 5  Made a variable y point to that 5 object  Incremented the reference count of the object with value 5  Created another integer object with the value 12  Subtracted the value of the object that y points to (5) from the value 12 in the (anonymous) object with that value  Assigned this value (7) to a new (so far, unnamed) integer object  Made the variable x point to this new object  Incremented the reference count of this new object that x points to
  • 12. Python Numbers This data type supports only numerical values like 1, 31.4, - 1000, 0.000023, 88888888. Python supports 3 different numerical types. 1. int - for integer values like 1, 100, 2255, -999999, 0, 12345678. 2. float - for floating-point values like 2.3, 3.14, 2.71, -11.0. 3. complex - for complex numbers like 3+2j, -2+2.3j, 10j, 4.5+3.14j.
  • 14. Augmented Assignment Operator  These operator allows you write shortcut assignment statements. For e.g:
  • 15. Bases  You can go the other direction, converting an integer to a string with any of these bases: >>> value = 65 >>> bin(value) '0b1000001' >>> oct(value) '0o101' >>> hex(value) '0x41’  The chr() function converts an integer to its single character string equivalent: >>> chr(65) ‘A’  And ord() goes the other way: >>> ord('A') 65
  • 16. Number Type Conversion  To change other Python data types to an integer, use the int() function. >>> int(True) 1 >>> int(False) 0  Turning this around, the bool() function returns the boolean equivalent of an integer: >>> bool(1) True >>> bool(0) False >>> int(98.6) 98 >>> int(1.0e4) 10000
  • 17. Number Type Conversion  To getting the integer value from a text string>>> int(True) >>> int(‘99') 99 >>> int('-23') -23 >>> int('+12') 12 >>> int('1_000_000') 1000000  If the string represents a nondecimal integer, you can include the base: >>> int('10', 2) # binary 2 >>> int('10', 8) # octal 8 >>> int('10', 16) # hexadecimal 16 >>> int('10', 22) # chesterdigital 22
  • 18. Number Type Conversion  you can convert a string containing characters that would be a valid float >>> float('98.6') 98.6 >>> float('-1.5') -1.5 >>> float('1.0e4') 10000.0  When you mix integers and floats, Python automatically promotes the integer values to float values >>> 43 + 2. 45.0  Python also promotes booleans to integers or floats: >>> False + 0 0 >>> False + 0. 0.0 >>> True + 0 1 >>> True + 0. 1.0
  • 19. Comment #  You’ll usually see a comment on a line by itself, as shown here: >>> # 60 sec/min * 60 min/hr * 24 hr/day >>> seconds_per_day = 86400  Or, on the same line as the code it’s commenting: >>> seconds_per_day = 86400 # 60 sec/min * 60 min/hr * 24 hr/day
  • 20. Continue Lines with >>> sum = 1 + ... 2 + ... 3 + ... 4 >>> sum 10 ------------------------------------------------------------------ ---- >>> sum = 1 + File "<stdin>", line 1 sum = 1 + ^ SyntaxError: invalid syntax >>> sum = ( ... 1 + ... 2 + ... 3 + ... 4) >>> >>> sum