Docsity

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

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