19-Ntu-Kc-Te-033 L-1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Talha Saif Lab-01 19-NTU-KC-TE-033

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.

It is employed for the following purposes:

• Web design and development (server-side),


• Creation of software
• Mathematics,
• Scripting for the operating system

What can Python do?


 Python can be used to construct web applications on a server.
 Python may be used to develop workflows in conjunction with other
software.
 Python has the ability to connect to database systems. It also has the ability
to read and change files.
 Python may be used to work with large amounts of data and execute
complex calculations.
 Python may be used for rapid prototyping as well as development of
production-ready software.

What makes Python so special?


 Python may be used on a variety of platforms (Windows, Mac, Linux,
Raspberry Pi, etc).
 Python has a basic grammar that is similar to that of English.
 Python's syntax enables programmers to write programmes in less lines than
other programming languages.
 Python is an interpreter language, which means that code can be run as soon
as it is written. As a result, prototyping can be done relatively quickly.

1|Page
Talha Saif Lab-01 19-NTU-KC-TE-033

 Python can be approached in three ways: procedural, object-oriented, and


functional.

Example 1:

print("Hello, World!")

Hello, World!

Example 2:

Print ("Hy, Boy!")

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:

print ("Six is greater than two!")

Six is greater than two

Python Data Types:

In programming, data type is an important concept.

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:

 Text Type: str

3|Page
Talha Saif Lab-01 19-NTU-KC-TE-033

 Numeric Types: int, float, complex


 Sequence Types: list, tuple, range
 Mapping Type: dict

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

To verify the type of any object in Python, use the type() function.

print(type(x))
print(type(y))
print(type(z))

Output:

6|Page
Talha Saif Lab-01 19-NTU-KC-TE-033

Int

Int, or integer, is a whole number, positive or negative, without decimals, of


unlimited length.

Integers:

Example 1:

x = 1
y = 35656222554887711
z = -3255522

print(type(x))
print(type(y))
print(type(z))
Output:

Float

Float, or "floating point number" is a number, positive or negative, containing one


or more decimals.

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 numbers are written with a "j" as the imaginary part:

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.

To make the code more readable, use comments.

When testing code, comments can be used to prevent execution.

To begin creating a comment in Python, we utilise the hash (#) symbol.

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

Comments starts with a #, and Python will ignore them:

Example:

#This is a comment
print("Hello, World!")

Output:

Hello, World!

Multi Line Comments


Multi-line comments aren't really supported in Python.
You might use a # for each line to add a multiline comment:
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Output:
Hello, World!

Or, not quite as intended, you can use a multiline string.

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.

Casting in python is therefore done using constructor functions:

 int() - constructs an integer number from an integer literal, a float literal


(by removing all decimals), or a string literal (providing the string
represents a whole number)
 float() - constructs a float number from an integer literal, a float literal or
a string literal (providing the string represents a float or an integer)
 str() - constructs a string from a wide variety of data types, including
strings, integer literals and float literals.

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:

x = float(1)     # x will be 1.0


y = float(2.8)   # y will be 2.8
z = float("3")   # z will be 3.0
w = float("4.2") # w will be 4.2

Output:

Example 3:

Strings:

11 | P a g e
Talha Saif Lab-01 19-NTU-KC-TE-033

x = str("s1") # x will be 's1'


y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'

Output:

Python Operators:
Variables and values are operated on with the use of operators.

The + operator is used to combine two values in the example below:

Example
print(10 + 5)

Output:

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common


mathematical operations:

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:

Python Identity Operators


Example 2:

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:

Operator Description Example

is Returns True if both variables are the same x is y


object

is not Returns True if both variables are not the same x is not y
object

Output:

Example 3:

Multiply 10 with 5, and print the result.


14 | P a g e
Talha Saif Lab-01 19-NTU-KC-TE-033

Output:

Print( 10*5 )

15 | P a g e

You might also like