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
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)
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("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("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
>>>
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
Post a Comment