Anda di halaman 1dari 23

PYTHON PROGRAMMING

ASSIGNMENT

NAME : P.ISWARYA.

ROLL NO: 18D3115.

CLASS : II MCA

DATE :
1. Python Installing in various operating system.

To get started working with Python 3, you’ll need to have access to


the Python interpreter.

There are several common ways to accomplish this:

 Python can be obtained from the Python Software


Foundation website at python.org. Typically, that involves
downloading the appropriate installer for your operating system and
running it on your machine.
 Some operating systems, notably Linux, provide a package
manager that can be run to install Python.
 On macOS, the best way to install Python 3 involves installing a
package manager called Homebrew
 In mobile operating systems like Android and iOS, you can install apps
that provide a Python programming environment.

Windows
How to install Python 3 on Windows:

Step 1: Download the Python 3 Installer

1. Open a browser window and navigate to the Download page for


Windows at python.org.
2. Underneath the heading at the top that says Python Releases for
Windows, click on the link for the Latest Python 3 Release - Python
3.x.x.
3. Scroll to the bottom and select either Windows x86-64 executable
installer for 64-bit or Windows x86 executable installer for 32-bit.

Step 2: Run the Installer


Once you have chosen and downloaded an installer, simply run it by double-
clicking on the downloaded file.

A dialog should appear that this looks something like:

Then just click Install Now.

Linux
There is a very good chance your Linux distribution has Python installed
already, but it probably won’t be the latest version, and it may be Python 2
instead of Python 3.

To find out what version(s) you have, open a terminal window and try the
following commands:

 python --version
 python2 --version
 python3 --version
One or more of these commands should respond with a version, as below:

$ python3 --version
Python 3.6.5

If the version shown is Python 2.x.x or a version of Python 3 that is not the
latest (3.6.5 as of this writing), then you will want to install the latest version.
The procedure for doing this will depend on the Linux distribution you are
running.

Ubuntu
Depending on the version of the Ubuntu distribution you run, the Python
install instructions vary. You can determine your local Ubuntu version by
running the following command:

$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial

Depending on the version number you see under Release in the console
output, follow the instructions below:

 Ubuntu 17.10, Ubuntu 18.04 (and above) come with Python 3.6 by
default. You should be able to invoke it with the command python3.
 Ubuntu 16.10 and 17.04 do not come with Python 3.6 by default, but it
is in the Universe repository. You should be able to install it with the
following commands:
 $sudo apt-get update
 $sudo apt-get install python3.6
You can then invoke it with the command python3.6.

 If you are using Ubuntu 14.04 or 16.04, Python 3.6 is not in the
Universe repository, and you need to get it from a Personal Package
Archive (PPA). For example, to install Python from the “deadsnakes” PPA,
do the following:
 $sudo add-apt-repository ppa:deadsnakes/ppa
 $sudo apt-get update
 $sudo apt-get install python3.6

Linux Mint

Mint and Ubuntu use the same package management system, which
frequently makes life easier. You can follow the instructions above for Ubuntu
14.04. The “deadsnakes” PPAworks with Mint.

Debian

We found sources that indicated that the Ubuntu 16.10 method would work
for Debian, but we never found a path to get it to work on Debian 9. Instead,
we ended up making Python from source as listed below.

One issue with Debian, however, is that it generally does not install
the sudo command by default. To install it, you’ll need to do the following
before you carry out the Compiling Python From Source instructions below:

$su
$apt-get install sudo
$vi /etc/sudoers
After that, open the /etc/sudoers file using the sudo vim command (or your
favorite text editor.) Add the following line of text to the end of the file,
replacing your_username with your actual username:

your_username ALL=(ALL) ALL


openSUSE

We found several sites describing how to get zypper to install the latest version
of Python, but they seemed problematic or outdated. We did not manage to
get any of them to work successfully, so we fell back to building Python from
source. To do that, you will need to install the development tools, which can
be done in YaST (via the menus) or by using zypper:

$suduzypper install -t pattern devel_C_C++

This step took a while and involved the installation of 154 packages, but once
it was completed, we were able to build the source as shown in the Compiling
Python FromSourcesection above.

CentOS

The IUS Community does a nice job of providing newer versions of software
for “Enterprise Linux” distros (i.e. Red Hat Enterprise and CentOS). You can use
their work to help you install Python 3.

To install, you should first update your system with the yum package manager:

$sudo yum update


$sudo yum install yum-utils
You can then install the CentOS IUS package which will get you up to date
with their site:

$sudo yum install https://centos7.iuscommunity.org/ius-release.rpm


Finally you can then install Python and Pip:

$sudo yum install python36u


$sudo yum install python36u-pip
Thanks to JaniKarhunen for his excellent writeup for CentOS 7.
Fedora

Fedora has a roadmap to switch to Python 3 as the default Python


published here. It indicates that the current version and the next few versions
will all ship with Python 2 as the default, but Python 3 will be installed. If
the python3 installed on your version is not 3.6, you can use the following
command to install it:

$sudodnf install python36

Arch Linux

Arch Linux is fairly aggressive about keeping up with Python releases. It is


likely you already have the latest version. If not, you can use this command:

$packman -S python

Compiling Python From Source

Step 1: Download the Source Code


To start, you need to get the Python source code. Python.org makes this fairly
easy. If you go to the Downloads page, you will see the latest source for
Python 3 at the top. (Make sure you don’t grab Legacy Python, Python 2.)

When you select the version, at the bottom of the page there is a Files section.
Select the Gzipped source tarball and download it to your machine. If you
prefer a command line method, you can easily use wget to download it to your
current directory:

$wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

Step 2: Prepare Your System


1. The first step you should take when doing an operation like this is to
update the system packages on your machine before you start
2. $sudo apt-get update
3. $sudo apt-get upgrade
4. Next, we want to make sure the system has the tools needed to build
Python. There are a bunch of them and you might already have some,
but that’s fine. I’ve listed them all in one command line, but you can
break the list into shorter commands by just repeating the sudo apt-get
install -y portion:
5. # For apt-based systems (like Debian, Ubuntu, and Mint)
6. $ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-
dev libncursesw5-dev xz-utilstk-dev
7.
8. # For yum-based systems (like CentOS)
9. $ sudo yum -y groupinstall development
10. $ sudo yum -y install zlib-devel

Step 3: Build Python


1. Once you have the prerequisites and the tar file, you can unpack the
source into a directory. Note that the following command will create a
new directory called Python-3.6.5 under the one you are in:
2. $ tar xvf Python-3.6.5.tgz
3. $cd Python-3.6.5
4. Now you need to run the ./configure tool to prepare the build:
5. $ ./configure --enable-optimizations --with-ensurepip=install
6. Next, you build the Python programs using make. The -j option simply
tells make to split the building into parallel steps to speed up the
compilation. Even with the parallel builds, this step can take a several
minutes:
7. $ make -j 8
8. Then, you’ll want to install your new version of Python. You’ll use
the altinstall target here in order to not overwrite the system’s version
of Python. Since you’re installing Python into /usr/bin, you’ll need to run
as root:
9. $sudo make altinstall
Step 4: Verify Your Python Install

Finally, you can test out your new Python version:

$ python3.6 -V

Python 3.6.5

macOS / Mac OS X
The best way we found to install Python 3 on macOS is through
the Homebrew package manager. This approach is also recommended by
community guides like The Hitchhiker’s Guide to Python.

Step 1: Install Homebrew (Part 1)

To get started, you first want to install Homebrew:

1. Open a browser and navigate to http://brew.sh/. After the page has


finished loading, select the Homebrew bootstrap code under “Install
Homebrew”. Then hit Cmd + C to copy it to the clipboard. Make sure
you’ve captured the text of the complete command because otherwise
the installation will fail.
2. Now you need to open a Terminal.app window, paste the Homebrew bootstrap
code, and then hit Enter . This will begin the Homebrew installation.
3. If you’re doing this on a fresh install of macOS, you may get a pop up
alert asking you to install Apple’s “command line developer tools”. You’ll need
those to continue with the installation, so please confirm the dialog box by
clicking on “Install”.
Step 2: Install Homebrew (Part 2)

You can continue installing Homebrew and then Python after the command
line developer tools installation is complete:

1. Confirm the “The software was installed” dialog from the developer
tools installer.
2. Back in the terminal, hit Enter to continue with the Homebrew
installation.
3. Homebrew asks you to enter your password so it can finalize the
installation. Enter your user account password and hit Enter to continue.
4. Depending on your internet connection, Homebrew will take a few
minutes to download its required files. Once the installation is complete,
you’ll end up back at the command prompt in your terminal window.

Step 3: Install Python

Once Homebrew has finished installing, return to your terminal and run the
following command:

$ brew install python3

This will download and install the latest version of Python. After the
Homebrew brew install command finishes, Python 3 should be installed on
your system.

You can make sure everything went correctly by testing if Python can be
accessed from the terminal:

1. Open the terminal by launching Terminal.app.


2. Type pip3 and hit Enter .
3. You should see the help text from Python’s “Pip” package manager. If
you get an error message running pip3, go through the Python install
steps again to make sure you have a working Python installation.
iOS (iPhone / iPad)
The Pythonista app for iOS is a full-fledged Python development environment
that you can run on your iPhone or iPad. It’s basically a combination of a
Python editor, documentation, and interpreter rolled into one single app.

Pythonista is surprisingly fun to use. It’s a great little tool when you’re stuck
without a laptop and want to work on your Python skills on the go. It comes
with the complete Python 3 standard library and even includes full
documentation you can browse offline.

To install and set up Pythonista you need to download it from the iOS app
store.

Android (Phones & Tablets)


If you have an Android tablet or phone and want to practice Python on the go,
there are a several options available. The one that we found most reliably
supports Python 3.6 is Pydroid 3.

Pydroid 3 features an interpreter you can use for REPL sessions, and it also
provides the ability to edit, save, and execute Python code.
2. Input
put and Output Statements.

A Program needs to interact with the user to accomplish the desired task;
this is done using Input-Output
Output facility.

 Input Statement:

Input means the data entered


e by the user of the program.
In python, we have input() and raw_input ( ) function available for Input.

1) input()
 If prompt is present, it is displayed on monitor, after which the user
can provide data from keyboard.
 Input takes whatever is typed from the keyboard and evaluates it.
 As the input provided is evaluated, it expects valid python expression.
 If the input provided is not correct then either syntax error or
exception is raised by python.

Syntax:

input (expression)

Example:

>>>x=input("Enter data:")
Enter data:34.78
>>>print(x)

34.78

2) raw_input()
This input method fairly works in older versions (like 2.x).

If prompt is present, it is displayed on the monitor after which user can


provide the data from keyboard.

The function takes exactly what is typed from keyboard, convert it to string
and then return it to the variable on LHS of '='.
Syntax:

raw_input (expression)

Example: In interactive mode

>>>x=raw_input('Enter your name: ')

Enter your name: ABC

x is a variable which will get the string (ABC), typed by user during the
execution of program. Typing of data for the raw_input function is
terminated by enter key.

We can use raw_input() to enter numeric data also. In that case we


typecast, i.e., change the data type using function, the string data accepted
from user to appropriate Numeric type.

Example:

>>>y=int(raw_input("Enter your roll no."))

Enter your roll no.5

It will convert the accepted string i.e., 5 to integer before assigning it


to 'y'.

 Print statement
Print evaluates the expression before printing it on the monitor.

Print statement outputs an entire (complete) line and then goes to next
line for subsequent output (s).

To print more than one item on a single line, comma (,) may be used.
Syntax:

print (expression/constant/variable)

Example:

>>> print ("Hello")

Hello

>>> print (5.5)

5.5

>>> print (4+6)

10

3. Operators in Python.

Operators are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the operand.

Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication etc.
Operator Meaning Example

x+y
+ Add two operands or unary plus +2

x-y
- Subtract right operand from the left or unary minus -2

* Multiply two operands x*y

/ Divide left operand by the right one x/y

% Modulus - remainder of the division of left operand by x % y (remainder


the right of x/y)

// Floor division - division that results into whole number


adjusted to the left in the number line x // y

x**y (x to the
** Exponent - left operand raised to the power of right power y)

Example #1: Arithmetic operators in Python


1. x = 15
2. y = 4
3.
4. # Output: x + y = 19
5. print('x + y =',x+y)
6.
7. # Output: x - y = 11
8. print('x - y =',x-y)
9.
10. # Output: x * y = 60
11. print('x * y =',x*y)
12.
13. # Output: x / y = 3.75
14. print('x / y =',x/y)
15.
16. # Output: x // y = 3
17. print('x // y =',x//y)
18.
19. # Output: x ** y = 50625
20. print('x ** y =',x**y)

When you run the program, the output will be:

x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

Comparison operators
Comparison operators are used to compare values. It either

returns True or False according to the condition.

Operator Meaning Example

> Greater that - True if left operand is greater than the right x>y

< Less that - True if left operand is less than the right x<y

== Equal to - True if both operands are equal x == y

!= Not equal to - True if operands are not equal x != y

Greater than or equal to - True if left operand is greater than or


>= equal to the right x >= y

Less than or equal to - True if left operand is less than or equal


<= to the right x <= y
Example #2: Comparison operators in Python
1. x = 10
2. y = 12
3.
4. # Output: x > y is False
5. print('x > y is',x>y)
6.
7. # Output: x < y is True
8. print('x < y is',x<y)
9.
10. # Output: x == y is False
11. print('x == y is',x==y)
12.
13. # Output: x != y is True
14. print('x != y is',x!=y)
15.
16. # Output: x >= y is False
17. print('x >= y is',x>=y)
18.
19. # Output: x <= y is True
20. print('x <= y is',x<=y)

Logical operators
Logical operators are the and, or, not operators.

Operator Meaning Example

and True if both the operands are true x and y

or True if either of the operands is true x or y

not True if operand is false (complements the operand) not x

Logical operators in Python

Example #3: Logical Operators in Python


1. x = True
2. y = False
3.
4. # Output: x and y is False
5. print('x and y is',xand y)
6.
7. # Output: x or y is True
8. print('x or y is',xor y)
9.
10. # Output: not x is False
11. print('not x is',not x)
Here is the truth table for these operators.

Bitwise operators
Bitwise operators act on operands as if they were string of binary digits. It operates
bit by bit, hence the name.

For example, 2 is 10 in binary and 7 is 111.


In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Operator Meaning Example

& Bitwise AND x& y = 0 (0000 0000)

| Bitwise OR x | y = 14 (0000 1110)

~ Bitwise NOT ~x = -11 (1111 0101)

^ Bitwise XOR x ^ y = 14 (0000 1110)

>> Bitwise right shift x>> 2 = 2 (0000 0010)

<< Bitwise left shift x<< 2 = 40 (0010 1000)

Assignment operators
Assignment operators are used in Python to assign values to variables.

a = 5 is a simple assignment operator that assigns the value 5 on the right to the
variable a on the left.
There are various compound operators in Python like a += 5 that adds to the
variable and later assigns the same. It is equivalent to a = a + 5.

Operator Example Equivatent to

= x=5 x=5

+= x += 5 x=x+5

-= x -= 5 x=x-5

*= x *= 5 x=x*5

/= x /= 5 x=x/5

%= x %= 5 x=x%5

//= x //= 5 x = x // 5

**= x **= 5 x = x ** 5

&= x &= 5 x=x&5

|= x |= 5 x=x|5

^= x ^= 5 x=x^5

>>= x >>= 5 x = x >> 5

<<= x <<= 5 x = x << 5

Special operators
Python language offers some special type of operators like the identity operator or
the membership operator.
They are described below with examples.

 Identity operators
is and is not are the identity operators in Python. They are used to
check if two values (or variables) are located on the same part of the memory. Two
variables that are equal does not imply that they are identical.

Operator Meaning Example

is True if the operands are identical (refer to the same object) x is True

True if the operands are not identical (do not refer to the x is not
is not same object) True

Example #4: Identity operators in Python


1. x1 = 5
2. y1 = 5
3. x2 = 'Hello'
4. y2 = 'Hello'
5. x3 = [1,2,3]
6. y3 = [1,2,3]
7.
8. # Output: False
9. print(x1 isnot y1)
10.
11. # Output: True
12. print(x2 is y2)
13.
14. # Output: False
15. print(x3 is y3)

Here, we see that x1 and y1 are integers of same values, so they are equal as well as
identical. Same is the case with x2 and y2 (strings).
But x3 and y3 are list. They are equal but not identical. It is because interpreter locates
them separately in memory although they are equal.

 Membership operators
in and not in are the membership operators in Python. They are used
to test whether a value or variable is found in a sequence
(string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.

Operator Meaning Example

in True if value/variable is found in the sequence 5 in x

not in True if value/variable is not found in the sequence 5 not in x

Example #5: Membership operators in Python


1. x = 'Hello world'
2. y = {1:'a',2:'b'}
3.
4. # Output: True
5. print('H'in x)
6.
7. # Output: True
8. print('hello'notin x)
9.
10. # Output: True
11. print(1in y)
12.
13. # Output: False
14. print('a'in y)

Here, 'H' is in x but 'hello' is not present in x (remember, Python is case sensitive).
Similary, 1 is key and 'a' is the value in dictionary y. Hence, 'a' in y returns False.

Anda mungkin juga menyukai