Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python basics control structures and loop statements, Lecture notes of Computer Science

Python basics control structures and loop statements if-else if-else statement: The if-elif-else statement is used to execute one of multiple blocks of code based on multiple conditions.

Typology: Lecture notes

2022/2023

Uploaded on 02/02/2023

ryda27
ryda27 🇮🇳

5

(1)

14 documents

1 / 57

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Need of programming Languages
Computers can execute tasks very rapidly and
assist humans
Programming languages Helps for
communication between human and machines.
They can handle a greater amount of input data.
But they cannot design a strategy to solve
problems for you
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39

Partial preview of the text

Download Python basics control structures and loop statements and more Lecture notes Computer Science in PDF only on Docsity!

Need of programming Languages

  • (^) Computers can execute tasks very rapidly and

assist humans

  • (^) Programming languages – Helps for

communication between human and machines.

  • (^) They can handle a greater amount of input data.
  • (^) But they cannot design a strategy to solve

problems for you

Python – Why?

  • (^) Simple syntax
  • (^) Programs are clear and easy to read
  • (^) Has powerful programming features
  • (^) Companies and organizations that use Python

include YouTube, Google, Yahoo, and NASA.

  • (^) Python is well supported and freely available at

www.python.org.

Python….

  • (^) High-level language ; other high-level languages you might have heard of are C, C++, Perl, and Java.
  • (^) There are also low-level languages , sometimes referred to as “machine languages” or “assembly languages.”
  • (^) Computers can only run programs written in low-level languages.
  • (^) So programs written in a high-level language have to be processed before they can run.

Python….

  • (^) Two kinds of program translator to convert from

high-level languages into low-level languages:

  • (^) Interpreters
  • (^) Compilers.
  • (^) An interpreter processes the program by

reading it line by line

Python….

  • (^) Python is considered an interpreted language because Python programs are executed by an interpreter.
  • (^) There are two ways to use the interpreter: interactive mode and script mode.

Python….Interactive mode In interactive mode, you type Python programs and the interpreter displays the result:

1 + 1 2 The shell prompt, >>>, is the prompt the interpreter uses to indicate that it is ready. If you type 1 + 1, the interpreter replies 2.

Python…. Script Mode

  • (^) Can also store code in a file and use the interpreter to execute the contents of the file, which is called a script.
  • (^) Python scripts have names that end with .py.
  • (^) Interactive mode is convenient for testing small pieces of code because you can type and execute them immediately.
  • (^) But for anything more than a few lines, should save your code as a script so you can modify and execute it in future.

Python….Script Mode …

File name : first.py print(4+3) print(4-3) print(4>3) print( "hello World" )

PAC For Chocolate Problem

Input Processing Output Amount in hand, N Price of one chocolate, C Number of wrappers for a free chocolate, M Number of Chocolates P = Quotient of N / C Free chocolate F = Quotient of P/M Total number of chocolates got by Bob

Pseudocode

• READ N and C

• COMPUTE num_of_chocolates as N/C

• CALCULATE returning_wrapper as number of

chocolates/m

• TRUNCATE decimal part of returning_wrapper

• COMPUTE Chocolates_recieved as

num_of_chocolates + returning_wrapper

• PRINT Chocolates_recieved

What is an Identifier?

• An identifier is a sequence of one or more

characters used to name a given program

element.

• In Python, an identifier may contain letters

and digits, but cannot begin with a digit.

• Special underscore character can also be used

• Example : line, salary, emp1, emp_salary

Rules for Identifier

  • (^) Python is case sensitive , thus, Line is different from line.
  • (^) Identifiers may contain letters and digits, but cannot

begin with a digit.

  • (^) The underscore character, _, is also allowed to aid in

the readability of long identifier names. It should not be

used as the first character

  • (^) Spaces are not allowed as part of an identifier.

Keywords

• A keyword is an identifier that has pre-defined

meaning in a programming language.

• Therefore, keywords cannot be used as “regular”

identifiers. Doing so will result in a syntax error,

as demonstrated in the attempted assignment to

keyword and below,

Keywords in Python