0% found this document useful (0 votes)
52 views

Structured of Programming Language: Using Python

This document provides an introduction to Python programming. It outlines topics students are expected to understand including installing Python, basic syntax, variables, data types, operators, comments, and getting user input. Key aspects of Python like being interpreted, interactive, readable, and relying on indentation are also covered at a high level.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Structured of Programming Language: Using Python

This document provides an introduction to Python programming. It outlines topics students are expected to understand including installing Python, basic syntax, variables, data types, operators, comments, and getting user input. Key aspects of Python like being interpreted, interactive, readable, and relying on indentation are also covered at a high level.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Structured of Programming Language

Using Python
LABORATORY

Introduction to Python Programming


At the end of the module the student is expected to:

• Install Python Define Variables


• Understand Python Basic Syntax Understand Data Types and Casting
• List Python Keywords List and Apply Python Operators
• Define Python Variables
• Define Python Statement
• Understand Indents in Python
• Understand Python Comments
• Appl Getting user input
• Install Python Variables
• Python Basic Syntax Data Types and Casting
• Python Keywords Operators
• Python Variables
• Python Statement
• Indents in Python
• Python Comments
• Getting user input
Visit

https://www.python.org/downloads/
• Python is Interpreted: Python is processed at runtime
by the interpreter. You do not need to compile your
program before executing it. This is similar to PERL
and PHP.
• Python is Interactive: You can actually sit at a Python
prompt and interact with the interpreter directly to
write your programs.
• 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.

Python is meant to be an easily readable language. Its
formatting is visually uncluttered, and it often uses English
keywords where other languages use punctuation. Unlike
many other languages, it does not use curly brackets to
delimit blocks, and semicolons after statements are optional.
It has fewer syntactic exceptions and special cases
than C or Pascal.
The following table list all the keywords in Python.
Except for the first
three (False, None and
True), the other
keywords are entirely
in lowercase
A Python variable is a reserved memory location to store
values. In other words, a variable in a python program
gives data to the computer for processing.
1. An identifier should start with either an alphabet letter (lower or upper case) or an
underscore (_).

2. After that, more than one alphabet letters (a-z or A-Z), digits (0-9) or underscores
may be used to form an identifier.

3. No other characters are allowed.

4. Conventionally, the name of the class begins with an uppercase alphabet letter.
Others start with lowercase alphabet letters.

5. Use of one or two underscore characters has a special significance when naming
the instance attributes of a class.
By default, the Python interpreter treats a piece of text
terminated by hard carriage return (new line character) as
one statement. It means each line in a Python script is a
statement. (Just as in C/C++/C#, a semicolon ; denotes the
end of a statement).

msg="Hello World"
code=123
name="Hadji"
However, you can show the text spread over more than one
lines to be a single statement by using the backslash (\) as a
continuation character. Look at the following examples:

Example: Continuation of Statement


msg ='Hello Pythonista'\
'Welcome to Python Tutorial Series'\
'from Hadji Tejuco'
Similarly, use the semicolon ; to write multiple statements in a
single line.

Example: Multiple Statements in Single Line

msg="Hello World";code=123;name="Hadji"
Python uses uniform indentation to denote a block of
statements.

a = 33
b = 200
if b > a:
print("b is greater than a")
In a Python script, the symbol # indicates the start of a comment line. It is
effective till the end of the line in the editor. If # is the first character of the
line, then the entire line is a comment. It can be used also in the middle of a
line. The text before it is a valid Python expression, while the text following is
treated as a comment.

# this is a comment
print ("Hello World")
print ("Welcome to Python Tutorial") #this is also a
comment but after a statement.
A triple quoted multi-line string is also treated as a comment if it is
not a docstring of a function or a class. (The use of docstring will
be explained in subsequent tutorials on Python functions.)
The input() function is a part of the core library of
standard Python distribution. It reads the key strokes as
a string object which can be referred to by a variable
having a suitable name.

name = input("Enter your name")


print (name)
num1 = eval(input("Enter first number "))
num2 = eval(input("Enter second number "))

avg = (num1+num2)/2
print ("the average of two numbers is ",(num1+num2)/2)
Any value of certain type is stored in the computer's
memory for processing.

In order to conveniently and repeatedly refer to the


stored value, it is given a suitable name. A value is
bound to a name by the assignment operator ‘=‘.

Example myVar=21
Data types are the classification or categorization of data
items. Data types represent a kind of value which determines
what operations can be performed on that data. Numeric,
non-numeric and Boolean (true/false) data are the most used
data types. However, each programming language has its
own classification largely reflecting its programming
philosophy.
Python has an in-built function type() to ascertain the data type of
a certain value. For example, enter type(1234) in Python shell
and it will return <class 'int'>, which means 1234 is an integer
value. Try and verify the data type of different values in Python
shell, as shown below.
#integer
x=2
print (x)
type (x) -> int

#float / double
y = 2.5
print (y)
type (y) -> float

#string -> words and letter


a = "Hello"
print (a)
type (a) -> str

b = 'hello'
print (b)

#logical --> True or False


q1 = True / False
print (q1)
type (q1) -> bool
# The arithmetic operators +, -, *, /, %, **, //
# ** Exponential calculation
# // Floor Division
print("5 + 2 =", 5+2)
print("5 - 2 =", 5-2)
print("5 * 2 =", 5*2)
print("5 / 2 =", 5/2)
print("5 % 2 =", 5%2)
print("5 ** 2 =", 5**2)
print("5 // 2 =", 5//2)
The comparison operators returns a boolean either True or False.

Assuming that x=10 and y=20, the result of the operations is also given in
following table:
The following keywords in Python combine two Boolean
expressions. They are called logical operators. Two operands
should have Boolean value True or False. Assuming that
x=True and y=False.
Comparison and logical
operators are useful in
controlling flow of program.

You might also like