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

Python - Basic Python Program

This document provides an overview of Python programming concepts including: 1) Python data types such as integers, floats, strings, lists, tuples, dictionaries, and sets. It also discusses variable naming rules and typecasting. 2) Core Python programming concepts like arithmetic operators, comparison operators, Boolean data type, immutability, formatting, comments, printing, and input/output. 3) Examples of basic Python programs that demonstrate taking input, performing calculations, and displaying output using formatting.

Uploaded by

Nabihah Husna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Python - Basic Python Program

This document provides an overview of Python programming concepts including: 1) Python data types such as integers, floats, strings, lists, tuples, dictionaries, and sets. It also discusses variable naming rules and typecasting. 2) Core Python programming concepts like arithmetic operators, comparison operators, Boolean data type, immutability, formatting, comments, printing, and input/output. 3) Examples of basic Python programs that demonstrate taking input, performing calculations, and displaying output using formatting.

Uploaded by

Nabihah Husna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PYTHON PROGRAMMING

(PYTHON 3.X using Jupyter Notebook)

DSC551 – PROGRAMMING FOR DATA SCIENCE

Pn Marhainis Jamaludin
Faculty of Computer and Mathematical Sciences
Python Data Types
• Variable Names
• can take any forms : numbers, letters, and underscore.
• Must begin with a letter or underscore
• Case sensitive
• Cannot use Python reserve words
• Native data types
• Floating point (float)
• Complex number - using j or the function complex()
• Integers (int and long)
• Boolean (bool)
• Strings (str)
• Lists (list)
• Tuples (tuple) - list but contain multiple pieces of data and mix data
types.
• Xrange (xrange) - useful during for loop
• Dictionary (dict) – used to pass options within functions
• Sets (set, frozenset) - collections which contains all unique
elements of a collection
Comment line
• Python will ignore all the lines which starts with # symbol
• # symbol is used for comment.
Printing Command
• Python 3.x uses function named print() to print or
display on screen.
Input from keyboard
• Python uses function named input() to get the
input from the keyboard.
Typecasting
• Python provides built-in function to typecast a
variable into another data type.
• The built-in function such as int(), str(), float()

• Use function type() to determine the data type of


current object
Initializing a variable
• A variable does not need explicitly declared.
• When a variable is assigned to a value, then a variable is declared
automatically.
• The assignment operator used is the equal operator (=)
• Use function type() to determine the current object data type
Multiple Assignment
• Python allows multiple assignment either assigning a single value to multiple
variables or assigning multiple values to multiple variables.

• Assign a single value to multiple variables. Example:

• Assign multiple values to multiple variables. Example:


Arithmetic Operators
Operator Description Example
+ Addition print (8 + 5)

- Subtraction print (10 – 5)

/ Division print (7 / 5)
Will yield float result

to yield integer result use // print (7 // 5)

** Exponentiation print (2 ** 3)

% Modulus Print (9 % 4)
Boolean Data Type
• Boolean data type (bool) in Python either True or
False (the first character must be capitalized)
Comparison Operators
Type Operator Description
Relational Operators < Less than
<= Less than and equal to
> Greater than
>= Greater than and equal to
== Equal
!= Not equal
Logical Operators and Both must be true than the result
will be true
or Either one must be true than the
result will be true
not The opposite
Special operator is Do two references refer to the same
object?
Immutability
• Everything in Python is an object.
• Python represents all its data as objects.
• Those mutable objects can be changed after it is created, however,
immutable objects cannot be changed.
• Mutable objects – container data types
• You can change the content without changing their identity
• Build-in types such as: list, set, dict
• For example lists → you can change the content of a lists, represent by square
brackets []

• Immutable objects - primitive data types


• You cannot change the content, once created, a fixed memory is assigned to it.
• Build-in types such as: int, float, bool, str, tuple, Unicode
• For example int → you cannot change number 1 to mean something else, however
you can add elements to a set which mutates the object, which can be represented
by a variable.
Another immutable example is tuple → a sequence similar to list, but contains
sequence of immutable Python objects. Represent by parentheses ()
Formatting
• Use of % within single quotes or double quotes
• ‘%s’ - for string
• ‘%d’ - for integer number
• ‘%nf - for decimal number where n is the length and
decimal places

Example:
subject = “DCS55
credhr = 3
cgpa = 3.25

print ( “This semester %s has %d credit hours and cgpa must be above %4.2f”
%(subject,credhr,cgpa)
Exercise 1:
• Write a python program to input two numbers,
sum those numbers and display the output. The
example is shown below:
Enter the first number: 45
Enter the second number : 60
The sum of two numbers is 105
Exercise 2:
• Given unitprice = 22.50, qty = 20, item = “Academic
Book”
• Calculate the total price to be paid where totalprice = unitprice
* qty
• Use the appropriate formatting to display as given below:
Academic Book price is RM22.50, Amran bought 20 books, the total price is
RM450.00

You might also like