SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Semelhante a python

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
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 

Semelhante a 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
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

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