0% found this document useful (0 votes)
13 views7 pages

Programming Basics in Python

Cambridge Computer Science - Programming short note
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Programming Basics in Python

Cambridge Computer Science - Programming short note
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming

 When algorithms are designed using flowcharts and pseudo codes, it will be
easy for the programmer to develop the code using a High Level
programming Language such as Python.

 Python IDLE allows us to write and run the code.

 There are 5 basic constructs in programming.

 Data use – constants,variables,arrays


 Sequence- order of steps in a task
 Selection – choosing a path through a program
 Iteration – repeating a sequence of steps in a program
 Operator use –arithmetic and logical

 What are variables?


Variables are named temporary storage locations that contains values
which are changing during the program execution.

 What are constants?


Constants are named fixed storage locations that contains a value which
does not change during program execution.

Cambridge - Computer Science Chapter 8


Basic data types

 Integer – Positive/negative whole numbers


 Real – positive /negative fractional part
 Char – variable/constant with a single character
 String - variable/constant with several characters
 Boolean - variable/constant that have ONLY 2 values
(true / false)

Input and Output

 All inputs default as strings, so if the input should be an integer / real


number , commands are used to change the data type.
Ex: In python – int( ) for integer
Float ( ) for real

 When displaying results as the output, several parts will be displayed with a
comma in between in python.
 When writing an algorithm following concepts should be used and
understood
- Sequence
- Selection
- Iteration
- Counting and totaling
- String handling
- Use of operators

Cambridge - Computer Science Chapter 8


Sequence

*correct order of steps are very important in an algorithm.

Trace table is used to view all outputs of an algorithm in each execution of


statement manually. This is called a dry run.

Selection

 Very useful technique.


 Allows to follow different routes through a choice.
 IF statement- for single choice with an alternative
CASE statement – for multiple choices

Iteration

 Following loop structures are used for repetitive tasks in the algorithm.
 Count – controlled loops (FOR loop)
 Condition-controlled loops
1) pre-condition (while loop)
2) post – condition (Do..while loop / Repeat.. until)

*Python ONLY uses - FOR (count-controlled)

WHILE loops (pre-condition).

Cambridge - Computer Science Chapter 8


Totalling and counting

These are standard methods.

In python –Ex:

Totalling :- Totalweight = Totalweight + weight

Counting :- Numberofitems +=1

Numberofitems=Numberofitems+1

String handling

 Strings are used to store text.


 Characters in a string are positioned starting from zero.

String manipulation methods

 Length – finding the number of characters


 Substring – extracting a part of the string
 Upper – converting all letters to capitals/upper case
 Lower – converting all letters to simple letters / lowercase

*These methods are provided by library routines.

Cambridge - Computer Science Chapter 8


Arithmetic , logical and Boolean operators
Arithmetic

+ Addition

- Subtraction

* Multiplication

/ Division

^ Power

MOD Remainder

DIV Division-Quotient

Logical

> Greater than

< Less than

= Equal

>= Greater than/equal

<= Less than/equal

<> Not equal

Boolean

AND Both true

OR Either true

NOT Not true

Cambridge - Computer Science Chapter 8


Subroutines (Procedures and functions)

 Instead of repeating statements and writing new code every time they are
required, it is essential to make use of subroutines.
 They are procedures and functions.

Procedure is a set of programming statements grouped together under a single


name that can be called to perform a task at any point in a program.

Function is a set of programming statements grouped together under a single


name that can be called to perform a task at any point in a program.
Comparatively, a function will return a value to the main program (procedure
does not).

What are parameters?

They are variables that store the values of arguments passed to a procedure or
function.

*When procedures or functions are identified, the 1 st statement in the definition


is a header.

Types of variables

Global variables can be used by any part of a program. It’sTheir scope covers
the entire program.

Local variables can only be used by the part of the program they have been
declared in. Their scope is restricted to that part of the program.

Library Routines

Each programming language has many library routines for standard tasks that
are commonly required by programs.

Ex:- MOD,DIV,ROUND,RANDOM

Cambridge - Computer Science Chapter 8


*A maintainable program should
1) use meaningful identifier names for variables, constants, arrays ,
procedures and functions
2) be divided into modules for each task using procedures or functions.
3) use comments

 An array is a data structure containing several elements of the same data


type, where it can be accessed by the same identifier name.

*One dimensional array – list


*Two dimensional array – table

What is the purpose of storing data in a file?

Data stored in RAM will be lost when the computer is switched off. Therefore ,
its important to keep the data permanently in files.

Cambridge - Computer Science Chapter 8

Common questions

Powered by AI

Python's string manipulation allows for flexible data handling, essential in text processing. Common operations include finding string length (length), extracting substrings (substring), converting case (upper, lower), and concatenation. These functions offer versatility in managing text content, enabling effective data parsing, formatting, and validation .

Variables are named storage locations whose values can change during program execution, allowing dynamic data manipulation. Constants, in contrast, maintain a fixed value, providing a stable reference point throughout execution. This dichotomy helps manage data efficiently, ensuring that some data remains consistent while allowing others to adapt as needed .

Python defaults inputs to strings, so converting input to the desired type (e.g., using int() for integers or float() for real numbers) ensures that operations like arithmetic are performed correctly. Neglecting this conversion leads to runtime errors or incorrect results, as incompatible operations on string data types produce unexpected behavior .

Logical operators, such as AND, OR, and NOT, are crucial for constructing compound boolean expressions that drive decision-making processes in conditional statements. AND ensures both conditions are true, OR requires at least one, and NOT negates a condition’s truth value. They enable complex logic flow control, crucial for algorithms that require nuanced condition evaluation .

Global variables have program-wide scope, accessible from any part, leading to potential dependencies that complicate debugging and maintenance. Local variables have limited scope, promoting modular and error-free coding by confining data to specific sections. This distinction influences memory usage and variable behavior, helping developers create predictable, secure, and maintainable code .

Flowcharts and pseudocode help simplify and visualize the problem-solving process, allowing developers to map out the logic before coding. They provide a clear step-by-step structure that aligns with Python’s syntax and semantics, making the transition to actual code smoother and reducing errors. This systematic approach also improves understanding and communication among programmers .

Subroutines, through procedures and functions, encapsulate code into reusable blocks, enhancing modularity. They reduce redundancy, as common tasks need not be rewritten, which streamlines updates and debugging. Functions add efficiency by returning values, enabling cleaner code with single-responsibility principles. This modularity simplifies complex programs and improves maintainability .

Data types define the operations that can be performed on the data and how it is stored in memory. Integers are used for whole numbers and perform arithmetic operations without decimals. Real numbers handle fractional values needed for precision. Strings manage textual data, supporting operations like concatenation and substring extraction. Choosing the correct type optimizes memory usage and function efficiency .

Pre-condition loops (like 'while' loops in Python) check the condition before executing the block, ensuring the code runs only if the condition is met initially, which could prevent unnecessary execution. Post-condition loops (like 'do-while' loops, though not in Python) run the block at least once regardless of the initial condition. Pre-condition loops can enhance efficiency by reducing iterations, while post-condition loops guarantee action execution, thus they are chosen based on algorithm requirements .

Arrays allow efficient storage and retrieval of data elements of the same type under a single identifier, supporting operations like iteration and sorting. One-dimensional arrays (lists) and two-dimensional arrays (tables) enable organized data handling, reducing complexity in algorithms that require grouped data manipulation. Arrays improve code readability and performance by structuring data operations succinctly .

You might also like