Python is a general-purpose, high-level programming language that can be used for web development, software development, data analysis, and more. It was created in the 1980s by Guido van Rossum. The document discusses why Python is useful, including that it is interpreted, dynamically typed, strongly typed, requires less code than other languages, and works on many platforms. Basic Python syntax and data types are also covered.
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.
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