SlideShare ist ein Scribd-Unternehmen logo
1 von 20
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

Weitere ähnliche Inhalte

Ähnlich wie python

Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonMaheshPandit16
 
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).pdfKosmikTech1
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in pythonsunilchute1
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha BaliAkanksha Bali
 
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
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfIda Lumintu
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data sciencedeepak teja
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of PythonElewayte
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxNimrahafzal1
 
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.pdfEjazAlam23
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Chariza Pladin
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
 

Ähnlich wie python (20)

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
 
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
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 

Kürzlich hochgeladen

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Kürzlich hochgeladen (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

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