Sie sind auf Seite 1von 11

A.M.

PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

Programming Languages:
A programming language is a vocabulary and set of grammatical rules for
instructing a computer or computing device to perform specific tasks. The
term programming language usually refers to
high-level languages, such
as BASIC, C, C++, COBOL, Java,FORTRA
N, Ada, and Pascal.

Each programming language has a unique set


of keywords (words that it understands) and a
special syntax for organizing
program instructions.

High-Level Programming Languages:


High-level programming languages, while simple compared to human languages,
are more complex than the languages the computer actually understands,
called machine languages. Each different type of CPU has its own unique
machine language.

Lying between machine languages and high-level languages are languages


called assembly languages. Assembly languages are similar to machine
languages, but they are much easier to program in because they allow
a programmer to substitute names for numbers. Machine languages consist of
numbers only.

Lying above high-level languages are languages called fourth-generation


languages (usually abbreviated 4GL). 4GLs are far removed from machine
languages and represent the class of computer languages closest to human
languages.
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

What is Python?
Python is an interpreted high-level programming
language for general-purpose programming. Created by Guido
van Rossum and first released in 1991, Python has a design philosophy that
emphasizes code readability, notably using significant whitespace.
Python is meant to be an easily readable language. Its formatting is visually
uncluttered, and it often uses English keywords where other languages use
punctuation. Unlike many other languages, it does not use curly brackets to
delimit blocks, and semicolons after statements are optional.

Reference implementation:
CPython is the reference implementation of Python. It is written in C, meeting
the C89 standard with several select C99 features. It compiles Python programs into an
intermediate bytecode which is then executed by its virtual machine. CPython is
distributed with a large standard library written in a mixture of C and native Python. It is
available for many platforms, including Windows and most modern Unix-like systems.
Platform portability was one of its earliest priorities.

Other implementations:
PyPy is a fast, compliant interpreter of Python 2.7 and 3.5. Its just-in-time
compiler brings a significant speed improvement over CPython.

Interesting fact: Python is named after the comedy television show Monty
Python’s Flying Circus. It is not named after the Python snake.

Features of Python programming language


Readable: Python is a very readable language.
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

Easy to Learn: Learning python is easy as this is a expressive and high level
programming language, which means it is easy to understand the language and
thus easy to learn.

Cross platform: Python is available and can run on various operating systems
such as Mac, Windows, Linux, Unix etc. This makes it a cross platform and
portable language.+

Open Source: Python is a open source programming language.

Large standard library: Python comes with a large standard library that has
some handy codes and functions which we can use while writing code in Python.

Free: Python is free to download and use. This means you can download it for
free and use it in your application.

Supports exception handling: If you are new, you may wonder what is an
exception? An exception is an event that can occur during program exception and
can disrupt the normal flow of program. Python supports exception handling
which means we can write less error prone code and can test various scenarios
that can cause an exception later on.

Advanced features: Supports generators and list comprehensions. We will


cover these features later
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

Automatic memory management: Python supports automatic memory


management which means the memory is cleared and freed automatically. You
do not have to bother clearing the memory.

In order to write a program using python language we need a IDE.

If you are new to programming then you may be wondering what do we mean by
IDE? IDE stands for integrated development environment. It is a software that
consolidates the basic tools that are required to write and test programs in a
certain language. Typically, an IDE contains a code editor, a compiler or
interpreter and a debugger that you can access at the same place through IDE
GUI.

Interactive Mode
When commands are read from a tty, the interpreter is said to be in interactive
mode. In this mode it prompts for the next command with the primary prompt,
usually three greater-than signs (>>>); for continuation lines it prompts with the
secondary prompt, by default three dots (...). The interpreter prints a welcome
message stating its version number and a copyright notice before printing the first
prompt:
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

Continuation lines are needed when entering a multi-line construct. As an example,


take a look at this if statement:

>>> the_world_is_flat = 1

>>> if the_world_is_flat:

... print "Be careful not to fall off!"

...

Be careful not to fall off!

LET US USE THE INTERPRETER AS A CALCULATOR


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
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

>>> 50 - 5*6

20

>>> (50 - 5.0*6) / 4

5.0

>>> 8 / 5.0

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.

The return type of a division (/) operation depends on its operands. If both
operands are of type int, floor division is performed and an int is returned. If either
operand is a float, classic division is performed and a float is returned. The //
operator is also provided for doing floor division no matter what the operands are.
The remainder can be calculated with the % operator:
>>> 17 / 3 # int / int -> int

>>> 17 / 3.0 # int / float -> float

5.666666666666667

>>> 17 // 3.0 # explicit floor division discards the


fractional part

5.0

>>> 17 % 3 # the % operator returns the remainder of


the division

>>> 5 * 3 + 2 # result * divisor + remainder


A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

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

There is full support for floating point; operators with mixed type operands convert
the integer operand to floating point:
>>> 3 * 3.75 / 1.5

7.5

>>> 7.0 / 2

3.5
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

In interactive mode, the last printed expression is assigned to the variable _. This
means that when you are using Python as a desk calculator, it is somewhat easier to
continue calculations, for example:

>>> tax = 12.5 / 100

>>> price = 100.50

>>> price * tax

12.5625

>>> price + _

113.0625

>>> round(_, 2)

113.06

This variable should be treated as read-only by the user. Don’t explicitly assign a
value to it — you would create an independent local variable with the same name
masking the built-in variable with its magic behavior.

In addition to int and float, Python supports other types of numbers, such as
Decimal and Fraction. Python also has built-in support for complex numbers, and
uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).

Let us introduced with math module and following functions:


NAME OF FUNCTIONS EXPLANATION
sin(...) sin(x) : Return the sine of x (measured in
radians).
cos(...) cos(x) : Return the cosine of x
(measured in radians).
tan(…) tan(x) : Return the tangent of x
(measured in radians).
asin(…) asin(x) : Return the arc sine (measured in
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

radians) of x.
acos(…) acos(x) : Return the arc cosine (measured
in radians) of x.
atan(…) atan(x) : Return the arc tangent (measured
in radians) of x.
radians(...) radians(x) :Convert angle x from degrees to
radians.
degrees(…) degrees(x) :Convert angle x from radians to
degrees.
sqrt(…) sqrt(x) : Return the square root of x.
pow(…) pow(x, y) :Return x**y (x to the power of
y).
exp(…) exp(x) :Return e raised to the power of x.
log(…) log(x[, base]) : Return the logarithm of x to
the given base.
If the base not specified, returns the
natural logarithm (base e) of x.

ceil(…) ceil(x) :Return the ceiling of x as a


float.This is the smallest integral value >=
x.
floor(…) floor(x) :Return the floor of x as a
float.This is the largest integral value <= x.
pi Give the floating value of 22/7
factorial(…) factorial(x) -> Integral
Find x!. Raise a ValueError if x is negative
or non-integral.
atan2(…) atan2(y, x) :Return the arc tangent
(measured in radians) of y/x.
Unlike atan(y/x), the signs of both x and y
are considered.
fabs(...) fabs(x) :Return the absolute value of the
float x.

Use all of the above functions in interactive mode with a suitable choice of x.
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

What is data type?


In computer science and computer programming, a data type or simply type is a
classification of data which tells the compiler or interpreter how the programmer
intends to use the data. A data type provides a set of values from which an
expression (i.e. variable, function...) may take its values.

You are already familiar with two data type, float and integer.Soon you will
become familiar with the other data types in python like
string,list,set,tuple,dictionary etc.

NB: Follow class for this section.

How to check the type of a variable?


Write type(variable name) then press enter key.

>>> a=2
>>> type(a)
<type 'int'>
>>> b=3.5
>>> type(b)
<type 'float'>
>>> a=[3,5,4,3]
>>> type(a)
<type 'list'>
>>> a="python"
>>> type(a)
<type 'str'>

How to make comment in a programe?

Use # sign to make a comment as below.


>>> # This is my first programe
A.M.PHYSICS LAB
MEHAK & AVIJIT-(8620967459,7685817762)

Das könnte Ihnen auch gefallen