0% found this document useful (0 votes)
8 views

Python 1

Python is an interpreted, high-level programming language. It has an interpreter that executes code line-by-line and a compiler that converts code into an executable file. Strings in Python are immutable and can be indexed, sliced, and operated on using operators like + and *. Strings support various string methods and built-in functions like len().

Uploaded by

9153am735443
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python 1

Python is an interpreted, high-level programming language. It has an interpreter that executes code line-by-line and a compiler that converts code into an executable file. Strings in Python are immutable and can be indexed, sliced, and operated on using operators like + and *. Strings support various string methods and built-in functions like len().

Uploaded by

9153am735443
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PYTHON

High level language - C, C++, Java, Fortran, Basic, Qbasic, Perl

(portable)

Low level language - machine language

(non-portable)

Interpreter

Compiler

example of print 1+1


Program - a sequence of instruction

Input

Output

Math

Conditional execution

Repitation

Debugging

Syntax error (grammar)

Runtime error

Semantic error
print "Hello 1st sem"

print 56

print 3.4

type ("Hello 1st sem")

type (54)

type (3.4)

type ("54")

type ("3.4")

print 1,000,000

Variable

message="get up guys"

n=17

pi=3.14

print message

print n

print pi

type (message)

type (n)

type (pi)
Mathematical operator

+ - * / ** % //

min=59

min/60

100*min/60

PEDMAS [B O D M A S]

operator with same precedence evaluated L to R

Mathematical operations on Strings

"apple"+"banana"

"Fun"*3 = "Fun"+"Fun"+"Fun"

http:/docs.python.org/27/tutorial/
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'

Indices may also be negative numbers, to start counting from the right:

>>> word[-1] # last character


'n'
>>> word[-2] # second-last character
'o'
>>> word[-6]
'P'

Note that since -0 is the same as 0, negative indices start from -1.

In addition to indexing, slicing is also supported. While indexing is used to obtain


individual characters, slicing allows you to obtain a substring:

>>> word[0:2] # characters from position 0 (included) to 2 (excluded)


'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'

Note how the start is always included, and the end always excluded. This makes
sure that s[:i] + s[i:] is always equal to s:

>>> word[:2] + word[2:]


'Python'
>>> word[:4] + word[4:]
'Python'

Slice indices have useful defaults; an omitted first index defaults to zero, an
omitted second index defaults to the size of the string being sliced.

>>> word[:2] # character from the beginning to position 2 (excluded)


'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'

One way to remember how slices work is to think of the indices as


pointing between characters, with the left edge of the first character numbered 0.
Then the right edge of the last character of a string of n characters has index n, for
example:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1

The first row of numbers gives the position of the indices 0…6 in the string; the
second row gives the corresponding negative indices. The slice from i to j consists of
all characters between the edges labeled i and j, respectively.

For non-negative indices, the length of a slice is the difference of the indices, if both
are within bounds. For example, the length of word[1:3] is 2.

Attempting to use an index that is too large will result in an error:

>>> word[42] # the word only has 6 characters


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range

However, out of range slice indexes are handled gracefully when used for slicing:

>>> word[4:42]
'on'
>>> word[42:]
''

Python strings cannot be changed — they are immutable. Therefore, assigning to


an indexed position in the string results in an error:

>>> word[0] = 'J'


...
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
...
TypeError: 'str' object does not support item assignment

If you need a different string, you should create a new one:

>>> 'J' + word[1:]


'Jython'
>>> word[:2] + 'py'
'Pypy'

The built-in function len() returns the length of a string:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34

You might also like