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

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

Microsoft BizTalk Server| Interview Questions| Set 2

Hi folks, We are back with set 2 of Biztalk server developer Interview Questions. Let's have a look then. State and explain stages of receive and send pipeline. Difference Between XML receive pipeline and passThru pipeline. State minimum components required in a pipeline. State maximum components used in a pipeline. Which property is required using Flat file dissambler and what happens if it is not set. What are the base types of pipeline components. What Interfaces are used for developing a general custom component pipeline. What Interfaces are used for implementing a dissambler custom pipeline component. How to execute a pipeline in an Orchestration. How to set properties of an adaptor dynamically in an Orchestration. What is message box and its purpose in Biztalk server. Types of subscription in Biztalk. Is it possible to have more than one port with same name. In which state can a send port do not subscribe to a message. Why multiple receive locations can

Microsoft C# - Basics

What is C#? We'll Take this definition from Wiki: Basic Points we should cover up: We would be learning following points in this Post: Writing and Reading from Console Variables, Data Types and Conversions Operators Conditions Passing Values to methods Handling Nullables Arrays, String, Struct and Enums Let's Code: The below Program will help you understand the basics of the above points listed. Go through each region separately and get to know the syntax of C#. We Believe, it's always better to Code and Learn than Read and Learn! If you want theoretical help related the basics, please visit here: C# Basics   Hope this helps to get you to start off with C# Coding! We would be adding more to this soon! And don't forget to visit  Joodle  to compile your codes online. Thanks! 😀