[go: up one dir, main page]

0% found this document useful (0 votes)
39 views17 pages

Lab 1

The document provides an overview of the Python programming language. Python is a high-level, interpreted, interactive and object-oriented scripting language that is designed to be highly readable. It can be used interactively from the interpreter prompt and for writing scripts. Key features covered include being interactive, object-oriented, a beginner's language, and a scripting language.

Uploaded by

Shayma Mostafa
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)
39 views17 pages

Lab 1

The document provides an overview of the Python programming language. Python is a high-level, interpreted, interactive and object-oriented scripting language that is designed to be highly readable. It can be used interactively from the interpreter prompt and for writing scripts. Key features covered include being interactive, object-oriented, a beginner's language, and a scripting language.

Uploaded by

Shayma Mostafa
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
You are on page 1/ 17

PYTHON

Python is a high-level, interpreted, interactive and object-oriented


scripting language. Python was designed to be highly readable
which uses English keywords frequently where as other languages
use punctuation and it has fewer syntactical constructions than
other languages.

Python is Interpreted: This means that it is processed at runtime by the interpreter and
you do not need to compile your program before executing it. This is similar to PERL and
PHP.
Python is Interactive: This means that you can actually sit at a Python prompt and
interact with the interpreter directly to write your programs.
Python is Object-Oriented: This means that Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
Python is Beginner's Language: Python is a great language for the beginner
programmers and supports the development of a wide range of applications from simple
text processing to WWW browsers to games.
Python is a script language: It’s a high-level programming language that
is interpreted by another program at runtime rather than compiled by the computer's
processor as other programming languages (such as C and C++).
WHY USE PYTHON INTERACTIVE
MODE
• If you want to know how something works, you can just try it. There is no
need to write up a file.

• Python is a very introspective programming language. If you want to know


anything about an object, you can just do dir(object).

• help(anything) for documentation. It's way faster than any web interface.

• Write a program / subprogram to use once, and then never again. The
fastest way to do this is to just do it in the Python interpreter.

• Debugging. You don't need to put selective print statements in code to


see what variables are when you write it in the interpreter. You just have
to type >>> a, and it will show what a is.
• The >>> prompt is the Python interpreter’s way of asking
you, “What do you want me to do next?”
• When we want to write a program, we use a text editor to
write the Python instructions into a file, which is called a
script. By convention, Python scripts have names that end
with .py
>>> print (4)
4
>>> type('Hello, World!')
<type ‘str’>
>>> type(17)
<type ‘int’>
>>> type(3.2)
<type ‘float’>

>>> print (1,000,000 )


100
Python interprets 1,000,000 as a comma separated sequence of
integers, which it prints with spaces between
This is the first example we have seen of a semantic error: the
code runs without producing an error message, but it doesn’t do
the “right” thing.
An assignment statement creates new variables and gives
them values:
>>> message = 'And now for something completely different'
>>> n = 17
>>> pi = 3.1415926535897931
To display the value of a variable, you can use a print
statement:
>>> print (n)
17
>>> print (pi)
3.14159265359
If you give a variable an illegal name, you get a syntax error:
>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax
RULES OF PRECEDENCE
Exponentiation has the next highest precedence,
so 2**1+1 is 3, not 4
3*1**3 is 3, not 27
• Multiplication and Division have the same precedence,
which is higher than Addition and Subtraction, which also
have the same precedence.
So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
• Operators with the same precedence are evaluated from left
to right.
So in the expression 5-3-1 is 1, not 3 because the 5-3
happens first and then 1 is subtracted from 2.
The modulus operator
>>> quotient = 7 / 3
>>> print (quotient)
2
>>> remainder = 7 % 3
>>> print (remainder)
1
The + operator works with strings, it performs concatenation,
which means joining the strings by linking them end-to-end.
>>> first = 10
>>> second = 15
>>> print (first + second)
25
>>> first = '100'
>>> second = '150'
>>> print (first + second)
100150
• Python provides a built-in function called input() that gets input
from the keyboard as in java
Scanner user_input = new Scanner( System.in );

• When this function is called, the program stops and waits for the
user to type something.

• When the user presses Return or Enter, the program resumes


and input() returns what the user typed as a string.

>>> x = input()
Anything
>>> print (x)
Anything
You can pass a string to input() to be displayed to the user before
pausing for input:
>>> name = input('What is your name?\n')
What is your name?
Chuck
>>> print (name)
Chuck

>>> prompt = 'What...is the airspeed velocity of an unlade swallow?\n'


>>> speed = input(prompt)
What...is the airspeed velocity of an unlade swallow?
17
>>> int(speed)
17
>>> int(speed) + 5
22
Comments, the # symbol

# compute the percentage of the hour that has elapsed


percentage = (minute * 100) / 60

percentage = (minute * 100) / 60 # percentage of an hour

v = 5 # assign 5 to v

v = 5 # velocity in meters/second.
BOOLEAN EXPRESSION
An expression that is either True or False. The following examples use the operator ==,
which compares two operands and produces True if they are equal and False otherwise:
>>> 5 = = 5
True
>>> 5 = = 6
False
True and False are special values that belong to the type bool; they are not
strings:
>>> type(True)
<type 'bool'>
x != y # x is not y
x >= y # x is greater than or equal to y
x>y # x is greater than y
x<y # x is less than an or equal to y
x <= y # x is less than or equal to y
x is y # x is the same as y
x is not y # x is not the same as y
Remember that = is an assignment
operator and == is a comparison operator. There is no such thing as =< or =>.
LOGICAL OPERATORS: AND, OR, NOT.

•x > 0 and x < 10 # AND


•n%2 == 0 or n%3 == 0 # OR
•not (x > y) # NOT

The operands of the logical operators should be


Boolean expressions, but Python is not very strict.
Any nonzero number is interpreted as “true.”
>>> 17 and True
True
EXERCISES
Exercise 2.1
Write a program that uses input() to prompt a user for their
name and then welcomes them.
Enter your name:
Chuck
Hello Chuck
Exercise 2.2
Write a program to prompt the user for hours and rate per
hour to compute gross pay.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
Exercise 2.3
Assume that we execute the following assignment
statements: width = 17 height = 12.0
For each of the following expressions, write the value of the
expression and the type (of the value of the expression).
1. width/2
2. width/2.0
3. height/3
4. 1 + 2 * 5
Use the Python interpreter to check your answers.
Exercise 2.4
Write a program which prompts the user for a Celsius
temperature, convert the temperature to Fahrenheit and print
out the converted temperature.
ADDITIONAL EXERCISES
1. Write one Python statement to print the asterisk pattern (*****) 5
times each on a separate line.
2. Write a Python program to display product of 10 and 5.
3. Write a Python program to find the sum and difference of any two
numbers.
4. Write a Python program to find out the area and circumference
given the radius of circle as an input.
5. Write a Python program to find out how many dozens and how
many extra oranges for a given number of oranges.
6. Write a Python program that receives two numbers from the user
and prints out both the average and the harmonic mean.
[Harmonic mean = 2 / ((1/x) + (1/y))].
7. Write a Python program that receives three inputs from users and
prints the product of three integers.
8. Write a Python program that computes a tip on a meal purchase.
Use 17 percent as the tipping rate.
9. Write a Python program that asks the user to type 2 integers A and
B and exchange the value of A and B.

You might also like