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

Sample Python

This document contains code snippets demonstrating various Python programming concepts like input/output, string manipulation, conditional statements, functions, loops, lists, sorting, recursion, object oriented programming, file handling, inheritance and more. Key concepts covered include getting user input, checking for palindromes, determining leap years, printing without newline, sorting lists, calculating factorials recursively, Fibonacci series, checking for anagrams, creating and accessing objects, counting string repetitions, reading/writing files, and removing duplicates from a list.

Uploaded by

radhika.cta
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Sample Python

This document contains code snippets demonstrating various Python programming concepts like input/output, string manipulation, conditional statements, functions, loops, lists, sorting, recursion, object oriented programming, file handling, inheritance and more. Key concepts covered include getting user input, checking for palindromes, determining leap years, printing without newline, sorting lists, calculating factorials recursively, Fibonacci series, checking for anagrams, creating and accessing objects, counting string repetitions, reading/writing files, and removing duplicates from a list.

Uploaded by

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

Getting user input:

====================
txt = input("ENter string")

Reverse string & palindrome:


============================
string = "baby"
resultant_string = ""
for iter in string:
resultant_string = iter + resultant_string

print (resultant_string)

if string == resultant_string:
print ("String is palindrome")
else:
print ("String is not palindrome")

Leap year:
==========
def is_leap(year):
leap = False

if (year % 400 == 0) and (year % 100 == 0):


leap = True
elif (year % 4 ==0) and (year % 100 != 0):
leap = True

return leap

year = int(raw_input())

Print without newline:


======================
print (iter, end ="")

Sorting:
========
l = [2,67,89,34,1,6,78,99,67]
rest_list = []

while l:
min = l[0]
for iter in l:
if iter < min:
min = iter
rest_list.append(min)
l.remove(min)

print ("sorted list is " + str(rest_list))

Sorting 2 :
===========
l = [64, 25, 12, 22, 11, 1,2,44,3,122, 23, 34]

for i in range(len(l)):
for j in range(i + 1, len(l)):
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]

print l

Factorial:
==========
input = 12
number = input + 1
list = []
for iter in range(number):
if iter == 0:
continue
list.append(iter)

print("list is " + str(list))


fact = 1
for iter in list:
fact = iter * fact

print ("Factorial is " + str(fact))

Fibanocci:
==========
def fib(numb):
if numb == 0:
return 0
elif numb == 1:
return 1
else:
return ((fib(numb-1)) + (fib(numb-2)))

get_number = 5
for item in range(0,get_number):
print(fib(item))

Anagram:
========
str1 = "paral"
str2 = "rapal"

# convert both the strings into lowercase


str1 = str1.lower()
str2 = str2.lower()

# check if length is same


if(len(str1) == len(str2)):

# sort the strings


sorted_str1 = sorted(str1)
sorted_str2 = sorted(str2)
print ("str 1 is " + str(sorted_str1))
print ("str 2 is " + str(sorted_str2))
# if sorted char arrays are same
if(sorted_str1 == sorted_str2):
print(str1 + " and " + str2 + " are anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")

Creating object:
================
## creating class
class mysample():
def __init__(self,name,age):
self.name=name
self.age=age

##Create object

object = mysample("jimmy", 36)


name_to_print = object.name
age_to_print = object.age

print (str(name_to_print) + " is of age " + str(age_to_print))

Str repeat:
============
txt = "Hello world"
x = re.findall("l", txt)

print ("list is " + str(x))


lenght = len(x)

print ("l repeated " + str(lenght) + " times")

File handling:
==============
import re
fhandle = open("sample.txt", "r")
print(fhandle.readline())
word_count = 0
for read_f in fhandle:
result = re.findall(r'\d+\.\d+\.\d+\.\d+', read_f)
word_count += len(result)

print ("IP address printed " + str(word_count) + " times")

Inheritance example:
=====================
# Parent class
class ParentClass:
def par_func(self):
print("I am parent class function")

# Child class
class ChildClass(ParentClass):
def child_func(self):
print("I am child class function")

# Driver code
obj1 = ChildClass()
obj1.par_func()
obj1.child_func()
Remove deuplicate from list using dict:
=======================================
input_list = [1,3,6,8,9,12,12,12,4,6,7,7,3,6]
print(input_list)
my_dict = {}
for item in range(0,len(input_list)):
my_dict[input_list[item]] = input_list[item+1]
if (int(item) == int(len(input_list)-2)):
break

print(my_dict)
print(my_dict.keys())

Physical -> converted to bitstreams of 01s


datalink -> packets to frames
Network -> breaks segments to packets
Transport -> data is segemented
Session -> opes session send/rece
Presentation -> sent using SMTP, compress data
Application

You might also like