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

Python Basics

The document contains examples of using Python to work with strings, lists, tuples, dictionaries, if/else statements, while loops, for loops and the else clause with loops. It demonstrates printing elements, slicing, concatenation, repetition and basic syntax for conditional and iterative control structures.

Uploaded by

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

Python Basics

The document contains examples of using Python to work with strings, lists, tuples, dictionaries, if/else statements, while loops, for loops and the else clause with loops. It demonstrates printing elements, slicing, concatenation, repetition and basic syntax for conditional and iterative control structures.

Uploaded by

bidhukes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python

Print

str = 'Hello World!'

print str # Prints complete string


print str[0] # Prints first character of the string

print str[2:5] # Prints characters starting from 3rd to 5th


print str[2:] # Prints string starting from 3rd character

print str * 2 # Prints string two times


print str + "TEST" # Prints concatenated string
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

List:

#!/usr/bin/python

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


tinylist = [123, 'john']

print list # Prints complete list

print list[0] # Prints first element of the list


print list[1:3] # Prints elements starting from 1nd till 2nd

print list[2:] # Prints elements starting from 3rd element


print tinylist * 2 # Prints list two times

print list + tinylist # Prints concatenated lists


['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']

Tuple:

#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )

tinytuple = (123, 'john')

print tuple # Prints complete list


print tuple[0] # Prints first element of the list

print tuple[1:3] # Prints elements starting from 2nd till 3rd


print tuple[2:] # Prints elements starting from 3rd element

print tinytuple * 2 # Prints list two times


print tuple + tinytuple # Prints concatenated lists
('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')
#!/usr/bin/python

dict = {}
dict['one'] = "This is one"

dict[2] = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

print dict['one'] # Prints value for 'one' key

print dict[2] # Prints value for 2 key


print tinydict # Prints complete dictionary

print tinydict.keys() # Prints all the keys


print tinydict.values() # Prints all the values
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']

If clause:

#!/usr/bin/python

var = 100

if ( var == 100 ) : print "Value of expression is 100"

print "Good bye!"


Value of expression is 100
Good bye!

While:

#!/usr/bin/python

count = 0

while (count < 9):


print 'The count is:', count

count = count + 1

print "Good bye!"


The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
Infinite loop:

#!/usr/bin/python

var = 1
while var == 1 : # This constructs an infinite loop

num = raw_input("Enter a number :")


print "You entered: ", num

print "Good bye!"


Enter a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3
You entered: 3
Enter a number between :Traceback (most recent call last):
File "test.py", line 5, in <module>
num = raw_input("Enter a number :")
KeyboardInterrupt

Above example goes in an infinite loop and you need to use CTRL+C to exit the program.

While else:

#!/usr/bin/python

count = 0
while count < 5:

print count, " is less than 5"


count = count + 1

else:
print count, " is not less than 5"
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

while clause consists only of a single statement, it may be placed on the same line as the while header.

while (flag): print 'Given flag is really true!'

For strings:

#!/usr/bin/python

for letter in 'Python': # First Example

print 'Current Letter :', letter

fruits = ['banana', 'apple', 'mango']


for fruit in fruits: # Second Example

print 'Current fruit :', fruit

print "Good bye!"


Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

Using else Statement with Loops:


#!/usr/bin/python

for num in range(10,20): #to iterate between 10 to 20


for i in range(2,num): #to iterate on the factors of the number

if num%i == 0: #to determine the first factor


j=num/i #to calculate the second factor

print '%d equals %d * %d' % (num,i,j)


break #to move to the next number, the #first FOR

else: # else part of the loop


print num, 'is a prime number'
10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 equals 3 * 5
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number

You might also like