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

Pythons notes for whoes students those study python very well, Lecture notes of Programming Languages

Python quick learning this type file also a student student very well and learn some python language

Typology: Lecture notes

2022/2023

Uploaded on 05/22/2023

aayush-tiwari-3
aayush-tiwari-3 🇮🇳

2 documents

1 / 89

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Introduction to Programming
with Python
Python Review. Modified slides from Marty Stepp and Moshe Goldstein
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
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59

Partial preview of the text

Download Pythons notes for whoes students those study python very well and more Lecture notes Programming Languages in PDF only on Docsity!

Introduction to Programming

with Python

Python Review. Modified slides from Marty Stepp and Moshe Goldstein

 code or source code : The sequence of instructions in a program.

 syntax : The set of legal structures and commands that can be

used in a particular programming language.

 output : The messages printed to the user by a program.

 console : The text box onto which output is printed.

 Some source code editors pop up the console as an external window, and others contain their own console window.

Programming basics

The Python Interpreter

  • Python is an interpreted

language

  • The interpreter provides

an interactive environment

to play with the language

  • Results of expressions are

printed on the screen

True

>>> 'print me'

'print me'

>>> print 'print me'

print me

Expressions

 expression : A data value or set of operations to compute a value.

Examples: 1 + 4 * 3 42

 Arithmetic operators we will use:

 + - * / addition, subtraction/negation, multiplication, division  % modulus, a.k.a. remainder  ** exponentiation

 precedence : Order in which operations are computed.

 * / % ** have a higher precedence than + -

1 + 3 * 4 is 13

 Parentheses can be used to force a certain order of evaluation. (1 + 3) * 4 is 16

Real numbers

 Python can also manipulate real numbers.

 Examples: 6.022 -15.9997 42.0 2.143e

 The operators + - * / % ** ( ) all work for real numbers.

 The / produces an exact answer: 15.0 / 2.0 is 7.  The same rules of precedence also apply to real numbers: Evaluate ( ) before * / % before + -

 When integers and reals are mixed, the result is a real number.

 Example: 1 / 2.0 is 0.

 The conversion occurs on a per-operator basis.  7 / 3 * 1.2 + 3 / 2  2 * 1.2 + 3 / 2  2.4 + 3 / 2  2.4 + 13.

Math commands

 Python has useful commands (or called functions) for performing

calculations.

 To use many of these commands, you must write the following at

the top of your Python program:

from math import *

Command name Description abs( value ) (^) absolute value ceil( value ) (^) rounds up cos( value ) (^) cosine, in radians floor( value ) (^) rounds down log( value ) (^) logarithm, base e log10( value ) (^) logarithm, base 10 max( value1 , value2 ) (^) larger of two values min( value1 , value2 ) (^) smaller of two values round( value ) (^) nearest whole number sin( value ) (^) sine, in radians sqrt( value ) (^) square root

Constant Description e (^) 2.7182818... pi (^) 3.1415926...

Variables

 variable : A named piece of memory that can store a value.

 Usage:  Compute an expression's result,  store that result into a variable,  and use that variable later in the program.

 assignment statement : Stores a value into a variable.

 Syntax:

name = value

 Examples: x = 5 gpa = 3.

x 5 gpa 3.

 A variable that has been given a value can be used in expressions. x + 4 is 9

 Exercise: Evaluate the quadratic equation for a given a , b , and c.

Example

>>> x = 7

>>> x

>>> x+

>>> x = 'hello'

>>> x

'hello'

Example: print Statement

>>> print 'hello'

hello

>>> print 'hello', 'there'

hello there

  • Elements separated by

commas print with a space

between them

  • A comma at the end of the

statement (print ‘hello’,)

will not print a newline

character

 input : Reads a number from user input.

 You can assign (store) the result of input into a variable.  Example: age = input("How old are you? ") print "Your age is", age print "You have", 65 - age, "years until retirement" Output: How old are you? 53 Your age is 53 You have 12 years until retirement

 Exercise: Write a Python program that prompts the user for

his/her amount of money, then reports how many Nintendo Wiis

the person can afford, and how much more money he/she will

need to afford an additional Wii.

input

Repetition (loops)

and Selection (if/else)

The for loop

for loop : Repeats a set of statements over a group of values.

 Syntax: for variableName in groupOfValues : statements  We indent the statements to be repeated with tabs or spaces.  variableName gives a name to each value, so you can refer to it in the statements.  groupOfValues can be a range of integers, specified with the range function.

 Example: for x in range(1, 6): print x, "squared is", x * x

Output: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25

Cumulative loops

 Some loops incrementally compute a value that is initialized outside

the loop. This is sometimes called a cumulative sum.

sum = 0 for i in range(1, 11): sum = sum + (i * i) print "sum of first 10 squares is", sum

Output: sum of first 10 squares is 385

 Exercise: Write a Python program that computes the factorial of an

integer.

if

 if statement : Executes a group of statements only if a certain

condition is true. Otherwise, the statements are skipped.

 Syntax: if condition : statements

 Example:

gpa = 3. if gpa > 2.0: print "Your application is accepted."