Lab-01-Python-Basics-11022025-023524pm
Lab-01-Python-Basics-11022025-023524pm
Lab-01-Python-Basics-11022025-023524pm
CEN-444
Lab-01:Python Basics
Spring 2025
Introduction to Python (& Installation), Anaconda Navigator, IDE (Spyder, Jupyter Notebook,
1A, 1B, 1C, 1D Pycharm)
Syntax, Basic Functions, Control Structures,
Loops & Functions, Classes & Inheritance,
***They can use any one of them
Modules in Python & Examples
Lists, Tuples, Sets & Dictionary, Numpy, Pandas Spyder, Numpy, Pandas
2A, 2B
13 Expert System, Forward Chaining, Backward Swish, Swipl, SwiPrologEditor Spyder, Pytholog
Chaining, Recursion
14 Constraint Satisfaction Problem (CSP) Spyder, Numpy, Pandas
Lab 1-A
Installing Python:
www.python.org
For Windows (32 bit): https://www.python.org/ftp/python/3.4.3/python-3.4.3.msi
Opening IDLE
Go to the start menu, find Python, and run the program labeled 'IDLE'
(Stands for Integrated DeveLopment Environment)
C++ Python
Indentation of code and Is Must be same for same block of code (for example for a set
use of white space irrelevant of statements to be executed after a particular if statement)
Parentheses for loop Required Not required but loop condition followed by a colon :
execution condition
while a < n:
print(a)
Calculations are simple with Python, and expression syntax is straightforward: the operators +, -,
* and / work as expected; parentheses () can be used for grouping.
# Python 3: Simple arithmetic
>>> 1 / 2
0.5
5.666666666666667
Python Operators
Command Name Example Output
+ Addition 4+5 9
- Subtraction 8-5 3
* Multiplication 4*5 20
% Modulus 19%3 5
** Exponent 2**4 16
Variables:
print ("This program is a demo of variables")
v = 1
v = v + 1
print ("To make v five times bigger, you would have to type v = v * 5")
v = v * 5
Strings:
word1 = "Good"
word2 = "Morning"
print (sentence)
Relational operators:
Expression Function
!= not equal to
== is equal to
Boolean Logic:
Boolean logic is used to make more complicated conditions for if statements that rely on more than
one condition. Python‘s Boolean operators are and, or, and not. The and operator takes two
arguments, and evaluates as True if, and only if, both of its arguments are True. Otherwise it
evaluates to False.
The or operator also takes two arguments. It evaluate if either (or both) of its arguments are
False.
Unlike the other operators we‘ve seen so far, not only takes one argument and inverts it. The result
of not True is False, and not False is True.
Operator Precedence:
Operator Description
() Parentheses
== != Equality Operators
Conditional Statements:
if' - Statement
y = 1
if y == 1:
if a > 5:
else:
„elif' - Statement
z = 4
if z > 70:
elif z < 7:
if x < 0:
x = 0
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
Lab 1-C
Input from user:
print (a)
Indexes of String:
Characters in a string are numbered with indexes starting at 0:
Example:
Index
0 1 2 3 4 5 6 7
Character
J . S m i t h
variableName [ index ]
Example:
print (name, " starts with", name[0])
Output:
input:
input: Reads a string of text from user input.
Example:
name = input("What's your name? ")
String Properties:
len(string) - number of characters in a string (including spaces)
str.lower(string) - lowercase version of a string
str.upper(string) - uppercase version of a string
Example:
name = "Linkin Park"
length = len(name)
big_name = str.upper(name)
Output:
LINKIN PARK has 11 characters
Characters map to numbers using standardized mappings such as ASCII and Unicode.
chr (number) - converts a number into a string.
Example: chr(99) is "c"
Loops in Python:
The 'while' loop
a = 0
a = a + 1
print (a )
print (i)
else:
Functions:
How to call a function?
function_name(parameters)
Code Example - Using a function
def multiplybytwo(x):
return x*2
a = multiplybytwo(70)
Define a Function?
def function_name(parameter_1,parameter_2):
{this is the code in the function}
return {value (e.g. text or number) to return to the main program}
range() Function:
If you need to iterate over a sequence of numbers, the built-in function range() comes in
handy. It generates iterator containing arithmetic progressions:
>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
It is possible to let the range start at another number, or to specify a different increment (even
negative; sometimes this is called the ‗step‘):
>>> range(5, 10)
[5, 6, 7, 8, 9]
Lambda Function:
Often, we quickly define mathematical functions with a one-line function called a lambda function.
Lambda functions are great because they enable us to write functions without having to name them, ie,
they're anonymous. No return statement is needed.
## Same as
hypotenuse(3,4)
Default Arguments:
Functions may also have default argument values. Functions with default values are used extensively in
many libraries. The default values are assigned when the function is defined.
def get_multiple(x, y=1):
return x, y, x*y
Note that you can use the name of the argument in functions, but you must either use all the names, or get
the position of the argument correct in the function call:
The word 'class' can be used when describing the code where the class is defined.
A variable inside a class is known as an Attribute
A function inside a class is known as a method
• A class is like a
– Prototype
– Blue-print
– An object creator
• A class defines potential objects
– What their structure will be
– What they will be able to do
• Objects are instances of a class
– An object is a container of data: attributes
– An object has associated functions: methods
Syntax:
# Defining a class
class class_name:
[statement 1]
[statement 2]
[statement 3] [etc]
Inheritance Syntax:
class child_class(parent_class):
def init (self,x):
# it will modify the _init_ function from parent class
# additional methods can be defined here
Example1:
class MyClass:
i = 12345
def f(self):
return 'hello world'
x = MyClass()
print (x.i)
print (x.f() )
Example2:
class Complex:
def init (self, realpart, imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
print (x.r," ",x.i )
Example3:
class Shape:
def init (self,x,y): #The init function always runs first
self.x = x
self.y = y
description = "This shape has not been described yet"
author = "Nobody has claimed to make this shape yet"
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
def describe(self,text):
self.description = text
def authorName(self,text):
self.author = text
def scaleSize(self,scale):
self.x = self.x * scale
self.y = self.y * scale
a=Shape(3,4)
print (a.area())
Inheritance Example:
class Square(Shape):
def init (self,x):
self.x = x
self.y = x
class DoubleSquare(Square):
def init (self,y):
self.x = 2 * y
self.y = y
def perimeter(self):
return 2 * self.x + 2 * self.y
Module:
A module is a python file that (generally) has only definitions of variables, functions, and
classes.
Example: Module name mymodule.py
def printdetails(self):
print ("This piano is a/an " + self.height + " foot")
print (self.type, "piano, " + self.age, "years old
and costing " +self.price + " dollars.")
import mymodule
print
(mymodule.ageo
fqueen )
cfcpiano =
mymodule.Piano
()
cfcpiano.print
details()