Skip to main content

Pylove


For the PyLovers , here we bring more coding examples. Written and tested by us, today we have 5 of the codes from basic to moderate that would help you get more attract towards #Python.

Here are some codes that we have been practicing. Hope you try them too!!!

Don't just copy+paste, edit too and see what changes can be done :)

1. Stringers:

In this code, we have created a function that will take any string as a input and perform various string operations using the inbuilt string function as follows:

1. length of string using len
2. First occurrence of a char in string using  .index
3. Counting the number of times the char as appeared using .count
4. Convert string to upper and lower case using .upper and .lower respectively
5. Checking if the string starts and ends with a particular letter using .startswith and .endswith 
6. Splitting the string using .split and storing it in a list.

THE CODE:





def stringers():

    s=input('Enter string')
    l=len(s)
    print('Length is',l)

    a=input('Enter the character you want to check and where does it occurs first')
    c=s.index(a)
    print('the char',a,'Occurs at position',c)

    w=input('Enter char you want to check how many time it repeats');
    d=s.count(w)
    print('the char',w,'repeats ',d,'times')

    print('We convert everythin to uppercase')
    u=s.upper()
    print(u)

    print('We convert everything to lowercase')
    l=s.lower()
    print(l)

    s1=input('Enter the letters to check string start with')
    if s.startswith(s1):
        print("TRUE")
    else:
        print("FALSE")

    s2=input('Enter the letters to check string ends with')
    if s.endswith(s2):
        print("TRUE")
    else:
        print("FALSE")

    print('Splitting the string')
    s3=s.split()
    print(s3)

NOTE: Indention are very important 

 
2. Check odd or even number



Here we define a function tp() and check(). This example shows us the nested functions thing. We pass the number through tp() to check() function that performs the normal math calculations and returns even or odd string.



THE CODE:







def tp():

    print('enter numberto check ')
    number=int(input('Ennter'))
    x=check(number)
    print(x)


def check(no):
    if(no%2==0):
        return "Even"
    elif(no%2!=0):
        return "Odd"
  





3. Sorting sorted:


This is a very simple code that takes in help of python inbuilt .sort() operator.
Using this .sort we can either put the string in reverse i.e. descending or ascending order. A code itself is quite explanatory.


THE CODE:


limit=10
a=[int(input('ener no.')) for i in range(limit)]

choice=int(input('Enter 1 for ascending sort and 2 for descending sort'))

if choice==1:
    a.sort()
    print(a)
elif choice==2:
    a.sort(reverse=True)
    print(a)


4. Password generator

So, what strikes you first when you think of password???
A combination of different things .. letters, digits ,symbols???

Python powers us with it's string library and gives us a way to use it to generate random things or passwords using a simple code as follows:

string.ascii_letters
Concatenation of the ascii (upper and lowercase) letters
 
string.digits
The string '0123456789'. 
 
string.punctuation
String of ASCII characters which are considered punctuation characters in the C 
locale
 
The above referred form python for beginners site is an easy way for password generation.

THE CODE:

import string
from random import *

char=string.ascii_letters+string.punctuation + string.digits

password = "".join(choice(char)for x in range(randint(8,18)))

print(password)



5. Current date and time getter:

A great thing that we just found out that python can be used to get the time and date that is of your cirrent system by simply using the datetime library .

THE CODE:


import datetime

now=datetime.datetime.now()

print("Current datte and time using the str method")
print(str(now))

print
print("Current data and time using attributes from instance")

print("Current year",now.year)
print("Current month",now.month)
print("Current day",now.day)
print("Current hour",now.hour)
print("Current min",now.minute)
print("Current second",now.second)
print("Current mircosecond",now.microsecond)

print
print("Current date and time using strftime")
print(now.strftime("%Y-%m-%d %H:%M"))


The last part is the formatted string time format and is to be followed in same manner to get correct output

THE OUTPUT:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Current date and time using the str method
2015-08-20 20:49:42.161753
Current data and time using attributes from instance
Current year 2015
Current month 8
Current day 20
Current hour 20
Current min 49
Current second 42
Current mircosecond 161753
Current date and time using strftime
2015-08-20 20:49
>>> 

Wait for more!!!
We will be continuing soon :)
Till then Learn.Share.Inspire :) :)

 

Comments

Popular posts from this blog

VLSI viva compilation

All the below stuff is a compiled post from google of all the viva questions asked on vlsi and avlsi , do take a look  I've actually made this for me :P ( Last minute Reading Stuff :) ;) ) 1. Why does the present VLSI circuits use MOSFETs instead of BJTs? Compared to BJTs, MOSFETs can be made very small as they occupy very small silicon area on IC chip and are relatively simple in terms of manufacturing. Moreover digital and memory ICs can be implemented with circuits that use only MOSFETs i.e. no resistors, diodes, etc. 2. What are the various regions of operation of MOSFET? How are those regions used? MOSFET has three regions of operation: the cut-off region, the triode region, and the saturation region. The cut-off region and the triode region are used to operate as switch. The saturation region is used to operate as amplifier. 3. What is threshold voltage? The value of voltage between Gate and Source i.e. V GS at which a sufficient number of mobile e...

Microsoft BizTalk Server | Interview Questions | Set 1

Hi Folks, Below is list of Some important questions that you may want to go through for a Biztalk developer role opening. Sharing the set 1 now just with questions. Will be sharing more soon. What is BizTalk Server? List down components of Biztalk server Biztalk architecture how is it? Types of schemas Document schema vs Envelope schema How to create envelope schema and its properties What is Property schema , how to create and its basic properties Purpose of using Flat file schema How to create a Flat file schema What do you mean by Canonical Schema What's is a message type Can a schema be without namespace What is min max and group min max property in a schema Explain Block default property Property promotion and types Distinguished field vs Promoted field Is it possible to promote XML record of complex content What is <Any> element in a schema Max length Promoted field and distinguished field What's Auto mapping and Default mapping  Can w...