Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

THE PYTHON - 3.An Informal Introduction to Python

Rating
-
Sold
-
Pages
12
Grade
A+
Uploaded on
03-05-2023
Written in
2022/2023

An Informal Introduction to Python

Institution
Course

Content preview

THE PYTHON

3. An Informal Introduction to Python:-
In the following examples, input and output are distinguished by the presence or
absence of prompts (>>> and …): to repeat the example, you must type everything
after the prompt, when the prompt appears; lines that do not begin with a prompt are
output from the interpreter. Note that a secondary prompt on a line by itself in an
example means you must type a blank line; this is used to end a multi-line command.

You can toggle the display of prompts and output by clicking on >>> in the upper-
right corner of an example box. If you hide the prompts and output for an example,
then you can easily copy and paste the input lines into your interpreter.

Many of the examples in this manual, even those entered at the interactive prompt,
include comments. Comments in Python start with the hash character, #, and extend
to the end of the physical line. A comment may appear at the start of a line or
following whitespace or code, but not within a string literal. A hash character within a
string literal is just a hash character. Since comments are to clarify code and are not
interpreted by Python, they may be omitted when typing in examples.

Some examples:

# this is the first comment
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."

3.1. Using Python as a Calculator:
Let’s try some simple Python commands. Start the interpreter and wait for the
primary prompt, >>>. (It shouldn’t take long.)
3.1.1. Numbers
The interpreter acts as a simple calculator: you can type an expression at it and it will
write the value. Expression syntax is straightforward: the operators +, -, * and / work
just like in most other languages (for example, Pascal or C); parentheses ( ()) can be
used for grouping. For example:

>>>
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0

, >>> # division always returns a floating point number
1.6

The integer numbers (e.g. 2, 4, 20) have type int, the ones with a fractional part
(e.g. 5.0, 1.6) have type float. We will see more about numeric types later in the
tutorial.

Division (/) always returns a float. To do floor division and get an integer result you
can use the // operator; to calculate the remainder you can use %:

>>>
>>> # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2 # floored quotient * divisor + remainder
17

With Python, it is possible to use the ** operator to calculate powers 1:

>>>
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128

The equal sign (=) is used to assign a value to a variable. Afterwards, no result is
displayed before the next interactive prompt:

>>>
>>> width = 20
>>> height = 5 * 9
>>> width * height
900

If a variable is not “defined” (assigned a value), trying to use it will give you an error:

>>>
>>> n # try to access an undefined variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

Written for

Institution
Course

Document information

Uploaded on
May 3, 2023
Number of pages
12
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$15.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
priyasaravana0222

Get to know the seller

Seller avatar
priyasaravana0222 TIRUPPUR KUMARAN COLLEGE FOR WOMEN
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
4
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions