Professional Documents
Culture Documents
19-Ntu-Kc-Te-033 L-1
19-Ntu-Kc-Te-033 L-1
19-Ntu-Kc-Te-033 L-1
Lap-01
Python Introduction:
What is Python?
Python is one of the most widely used programming languages. Guido van Rossum
created it, and it was released in 1991.
1|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
Example 1:
print("Hello, World!")
Hello, World!
Example 2:
Hy , Boy
Example 3:
print("Bye, Yes")
Bye, Yes
Python Syntax:
Python syntax can be executed directly on the Command Line, as we learnt on the
previous page.
Or by creating a python file on the server, using the .py file extension, and running
it in the Command Line:
Example 1:
2|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
if 5 > 2:
print("Five is greater than two!")
Example 2:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Example 3:
if 6 > 2:
Variables can store a variety of data, and different types can perform different
tasks.
Python comes with the following data types pre-installed in these categories:
3|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
Example 1:
Example 2:
Example 3:
Python Variables:
A variable is a named location in memory where data is stored. Variables can be
thought of as a container for data that can be altered later in the programme.
4|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
Example1:
Output:
Example2:
As shown in the above example, the assignment operator = can be used to assign a
value to a variable.
5|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
Example3:
Python Numbers:
There are three numeric types in Python:
int
float
complex
Variables of numeric types are created when you assign a value to them:
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
Output:
6|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
Int
Integers:
Example 1:
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Output:
Float
Floats:
Example 2:
x = 1.10
y = 1.0
z = -35.59
7|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
print(type(x))
print(type(y))
print(type(z))
Output:
Complex
Complex:
Example 3:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Output:
Python Comments:
Python code can be explained using comments.
8|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
It goes all the way to the newline character. Comments help programmers
understand a programme better. Comments are ignored by the Python interpreter.
Creating a Comment
Example:
#This is a comment
print("Hello, World!")
Output:
Hello, World!
Since Python will ignore string literals that are not assigned to a variable, you can
add a multiline string (triple quotes) in your code, and place your comment inside
it:
Example 1:
"""
This is a comment
written in
more than just one line
9|Page
Talha Saif Lab-01 19-NTU-KC-TE-033
"""
print("Hello, World!")
Output:
Hello, World!
Example 2:
# This is the comment
Example3:
Python Casting:
Specify a Variable Type
There may be times when you want to specify a type on to a variable. This can be
done with casting. Python is an object-orientated language, and as such it uses
classes to define data types, including its primitive types.
10 | P a g e
Talha Saif Lab-01 19-NTU-KC-TE-033
Example 1:
Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
Output:
Example 2:
Floats:
Output:
Example 3:
Strings:
11 | P a g e
Talha Saif Lab-01 19-NTU-KC-TE-033
Output:
Python Operators:
Variables and values are operated on with the use of operators.
Example
print(10 + 5)
Output:
12 | P a g e
Talha Saif Lab-01 19-NTU-KC-TE-033
Example 1:
13 | P a g e
Talha Saif Lab-01 19-NTU-KC-TE-033
Output:
Identity operators are used to compare the objects, not if they are equal, but if they
are actually the same object, with the same memory location:
is not Returns True if both variables are not the same x is not y
object
Output:
Example 3:
Output:
Print( 10*5 )
15 | P a g e