Anda di halaman 1dari 11

De La Salle University - Dasmariñas 1

CEAT – Engineering Department

De La Salle University Dasmariñas


College of Engineering, Architecture and
Technology

T-CPET121LA
Introduction to
Computational Thinking
(Python)

Prepared by:
Joshua T. Isaguirre

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 2
CEAT – Engineering Department

Manual Contents
INTRODUCTION TO PYTHON PROGRAMMING ................................................................................ 3
What is Java? ..................................................................................................................................... 3
PYTHON FUNDAMENTALS................................................................. Error! Bookmark not defined.
Variable Casting ................................................................................ Error! Bookmark not defined.
Global vs Local Variables................................................................................................................... 6
Deleting Variables .............................................................................................................................. 7
Keywords ............................................................................................................................................ 7
Operators .......................................................................................... Error! Bookmark not defined.

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 3
CEAT – Engineering Department

INTRODUCTION TO PYTHON PROGRAMMING


Review of Program Development Cycle
The diagram below illustrates how a program is developed. (This is introduced on Programming Logic
and Design)

This course is all about writing the code, translating the code and testing the program. There are two
levels of programming language, the High Level and the Low Level programming language.
 Low-level programming language (Machine language) – The language of a computer. It is a
sequence of 0s and 1s since digital signals are processed inside a computer. The fundamental
language of the computer’s processor.
 High-level programming language – Uses English like statements.

The higher-level language code is called source code. The corresponding machine language code is
called the target code. Programmers have a variety of tools available to enhance the software
development process. Some common tools include:
 Editors – An editor allows the programmer to enter the program source code and save it to
files. The syntax of a language refers to the way pieces of the language are arranged to make
well-formed sentences.
 Compilers – A compiler translates the source code to target code and produce an executable
program.
 Interpreters – An interpreter is like a compiler. While a compiler produces an executable
program that may run many times with no additional translation needed, an interpreter
translates source code statements into machine language each time a user runs the program.
A compiled program does not need to be recompiled to run, but an interpreted program must
be reinterpreted each time it executes. For this reason interpreted languages are often referred

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 4
CEAT – Engineering Department

to as scripting languages. The interpreter in essence reads the script, where the script is the
source code of the program. Python is an interpreted language.
 Debugger – A debugger allows a programmer to more easily trace a program’s execution in
order to locate and correct errors in the program’s implementation. A developer can
simultaneously run a program and see which line in the source code is responsible for the
program’s current actions.
 Profilers – A profiler collects statistics about a program’s execution allowing developers to
tune appropriate parts of the program to improve its overall performance. A profiler indicates
how many times a portion of a program is executed during a particular run, and how long that
portion takes to execute.

What is Python
Guido van Rossum created the Python programming language in the
late 1980s. In contrast to other popular languages such as C, C++, Java,
and C#, Python strives to provide a simple but powerful syntax.

Back in the 1970s, there was a popular BBC comedy tv show called Monty
Python’s Fly Circus and Van Rossum happened to be the big fan of that show.
So when Python was developed, Rossum named the project ‘Python’.

Why Python for beginners?


Less Syntax Memorization
Given the simplicity of Python’s syntax, you won’t
need to memorize lots of sections code that are
included in many different places. With less code to
memorize, there are fewer mistakes made by a
developer.

Easy to Learn, Read, and Use


Unlike C# and other languages, Python’s syntax is
human readable and it’s concise. As a beginner, this
will allow you pick up the basics quickly, with less
mental strain, and you can level up to advanced topics
quicker. With one glance at Python code, you can
infer what the code is doing. In contrast, most
programming languages require more syntax (written)
code to accomplish similar tasks, and the syntax
doesn’t mirror the human language.

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 5
CEAT – Engineering Department

Proper Programming Practices are Built-in


When learning new concepts, it is a great
time to learn about industry standards and
proper programming practices. Python has
you covered. There ar e a ton of
programming standards to follow but it
always will come down to the basics. The
practice of indenting your code is a perfect
example. This allows you to stay organized
and makes it easier for developers to read
your code base. It also is required in Python.
For Python to be interpreted, indentation tells our compiler how our code base is meant to be run.

Python is an Object-Oriented Language


Every developer needs to know about Object-Oriented Programming and it comes built into the Python
language. This is the practice of creating objects and data to solve problems.

In-demand Language
None of this would matter if Python was not relatable to the industry. Python is widely used in Data
Science, Web Applications or Game Development

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 6
CEAT – Engineering Department

VARIABLES
Variable is a primary storage location that holds different numeric or alphanumeric values. It is also a
memory space allocated by a computer user for processing and storing data. In most of the
programming languages a variable is a named location used to store data in the memory. Each
variable must have a unique name called identifier.

Initiating Variables
In Python, variables do not need declaration to reserve memory space. The "variable declaration" or
"variable initialization" happens automatically when we assign a value to a variable.
START
Declarations
int A = 5
float B = 10.5
string C = "Hello"
Boolean D = True
STOP
A = 5
B = 10.5
C = 'Hello'
D = True
or
A,B,C,D = 5,10.5,'Hello',True

Implicit vs Explicit Conversion


In Implicit type conversion is when a Python automatically converts one data type to another data
type.
A = 100
B = 10.5
print('Datatype of A: ', type(A))
print('Datatype of B: ', type(B))
C = A + B
print('Datatype of C: ', type(C))

In Explicit Type Conversion, users convert the data type of an object to required data type. We use
the predefined functions like int(), float(), str(), etc to perform explicit type conversion. This
type conversion is also called typecasting because the user casts (change) the data type of the
objects.
A = '100'
print('Datatype of A: ', type(A))
A = int(A)
C = A + 100
print('Datatype of B: ', type(A))
print('Datatype of B: ', type(C))

Use the function type() to identify the type of variable.

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 7
CEAT – Engineering Department

Global vs Local Variables


Global variables are variables that are declared globally and can be accessed by the entire program.
Local variables are variables that are declared inside a function or block and can be accessed only
in its scope.

Deleting Variables
Deleting variables removing the memory allocation for the variable. Use the command del for deleting
variable.
X = 'Hello World'
print(X)
del X
print(X)

Keywords
Keywords are the reserved words in Python. We cannot use a keyword as variable name, function
name or any other identifier. They are used to define the syntax and structure of the Python language.
In Python, keywords are case sensitive.

False Class finally is return None continue

True for lambda try def from nonlocal

while and del global not with as

assert if elif or yield in raise

break else except import pass

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 8
CEAT – Engineering Department

I/O STREAMS AND OPERATORS


Output Streams
To display the output on the output screen, the syntax is:

print(message)

Where:
 message is the string (enclosed by ‘ ‘), variable, or combination of both (concatenated by +) to
be displayed

EXAMPLE: Display your name and course.


START
Declarations
Output(“Joshua Isaguirre”)
Output(“BS Computer Engineering”)
STOP
print('Joshua Isaguirre')
print('BS. Computer Engineering')

START
Declarations
String name = “Joshua Isaguirre”
String course = “BS Computer Engineering”
Output(“My Name is: ” & name)
Output(“My Course is: ” & course)
STOP

name = 'Joshua Isaguirre'


course = 'BS. Computer Engineering'
print('My name is: ', name)
print('My course is: ', course)

EXAMPLE: Print the word “Hello World” as a quote


START
Declarations
Output(““Hello World””)
STOP
print('\"Hello World\"')

Input Streams
To receive an input from the user, the syntax is:

X = input(prompt)

Where:
 X is the variable that will receive the input value

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 9
CEAT – Engineering Department

 prompt is the message to display in the screen (Optional).

EXAMPLE: Input your name and age, and display it


START
Declarations
String NameA
int intA
Output(“Input your name: ”)
Input(NameA)
Output(“Input your age: ”)
Input(intA)
Output(“Your Name is: ” & NameA & “, and you are ” & intA & “ years old”
STOP
NameA = input('Input your name: ')
intA = input('Input your age: ')
print('Your name is',NameA + ', and you are',intA + ' years old')

Operators
Operators are syntaxes and symbols used to manipulate and control inputs and variables to produce
the desired output.

Arithmetic Operators

Operator Description Example

+ Addition – Adds values on either side of the operator. a + b = 30


Subtraction – Subtracts right hand operand from left
- a – b = -10
hand operand.
Multiplication – Multiplies values on either side of the
* a * b = 200
operator
Division – Divides left hand operand by right hand
/ b/a=2
operand
Modulo – left hand operand by right hand operand
% b%a=0
and returns remainder
Exponent – Performs exponential (power) calculation
** a**b =10 to the power 20
on operators
Floor Division – The division of operands where the
result is the quotient in which the digits after the 9//2 = 4 and 9.0//2.0 =
// decimal point are removed. But if one of the 4.0, -11//3 = -4, -11.0//3 =
operands is negative, the result is floored, i.e., -4.0
rounded away from zero (towards negative infinity) −

Assignment Operators

Operator Description Example

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 10
CEAT – Engineering Department

Assigns values from right side operands to left side c = a + b assigns value of
=
operand a + b into c

Add AND – It adds right operand to the left operand c += a is equivalent to


+=
and assign the result to left operand c=c+a

Subtract AND – It subtracts right operand from the c -= a is equivalent to


-=
left operand and assign the result to left operand c=c-a

Multiply AND – It multiplies right operand with the left c *= a is equivalent to


*=
operand and assign the result to left operand c=c*a

Divide AND – It divides left operand with the right c /= a is equivalent to


/=
operand and assign the result to left operand c=c/a

Modulus AND – It takes modulus using two operands c %= a is equivalent to


%=
and assign the result to left operand c=c%a

Exponent AND – Performs exponential (power)


c **= a is equivalent to c
**= calculation on operators and assign value to the left
= c ** a
operand

Floor Division AND – It performs floor division on c //= a is equivalent to c =


//=
operators and assign value to the left operand c // a

Relational Operators

Operator Description Example

If the values of two operands are equal, then the


== (a == b) is not true.
condition becomes true.

If values of two operands are not equal, then


!= (a != b) is true.
condition becomes true.

If values of two operands are not equal, then (a <> b) is true. This is
<>
condition becomes true. similar to != operator.

If the value of left operand is greater than the value


> (a > b) is not true.
of right operand, then condition becomes true.

If the value of left operand is less than the value of


< (a < b) is true.
right operand, then condition becomes true.

If the value of left operand is greater than or equal to


>= the value of right operand, then condition becomes (a >= b) is not true.
true.

If the value of left operand is less than or equal to the


<= (a <= b) is true.
value of right operand, then condition becomes true.

T-CPET121LA
Introduction to Computational Thinking
De La Salle University - Dasmariñas 11
CEAT – Engineering Department

Logical Operators

Operator Description Example

and Logical If both the operands are true then condition becomes
(a and b) is true.
AND true.

or Logical If any of the two operands are non-zero then


(a or b) is true.
OR condition becomes true.

not Logical
Used to reverse the logical state of its operand. Not(a and b) is false.
NOT

EXAMPLE: Input two numbers and display their sum, difference, multiplication and division
START
Declarations
int A, B, sum, diff, prod, quo
Output(“Enter first number: ”)
Input(A)
Output(“Enter second number: ”)
Input(B)
sum = A + B
diff = A – B
prod = A * B
quo = A / B
Output(“The sum is: ” & sum)
Output(“The difference is: ” & diff)
Output(“The product is: ” & prod)
Output(“The quotient is: ” & quo)
STOP
A = int(input('Enter first number: '))
B = int(input('Enter second number: '))
sum = A + B
diff = A - B
prod = A * B
quo = A / B
print('The sum is: ', sum)
print('The difference is: ', diff)
print('The product is: ', prod)
print('The quotient is: ', quo)

T-CPET121LA
Introduction to Computational Thinking

Anda mungkin juga menyukai