f = open(‘temp.txt’,’r’) d=0u=0l=0s=0sp=0lines=0content = f.read() for ch in content: print(‘Lower case ‘,l)print(‘Upper case’,u)print(‘Spaces ‘,s)print(‘Digits ‘,d)print(‘Special ‘,sp)print(‘Lines ‘,lines) Output: Lower case 18Upper case 4Spaces 4Digits 2Special 1Lines 2 input file Good Morning Good Evening23 *
Category Archives: Python
Check signed even odd number
Using nested if , program is checking given number is positive even/odd number or negative even/odd number. num = int(input(‘Enter number: ‘)) if num > 0: if num % 2 ==0: print(num,’ is +ve even number. ‘) else: print(num, ‘ is + odd number. ‘)else: if num % 2 == 0: print(num,’ -ve even numberContinue reading “Check signed even odd number”
Multiple ways to calculate 8 raise to 7
import math print(int(math.pow(8,7))) print(8 ** 7) s=1 for i in range(1,8): s=s * 8 print(s) k=1s=1 while k < 8: s=s*8 k=k+1 print(s) Output 2097152209715220971522097152
Employee list in python
employees=[[1,’John’,’Admin’,’Manager’],[2,’Michael’,’Finance’,’Clerk’],[3,’Tom’,’Marketing’,’Supervisor’]] for emp in employees: print(emp) Output [1, ‘John’, ‘Admin’, ‘Manager’][2, ‘Michael’, ‘Finance’, ‘Clerk’][3, ‘Tom’, ‘Marketing’, ‘Supervisor’]
Display float values from list
numbers = [1,1.3,3,4,5.5,6.5,8.9,99] for n in numbers: if type(n) == float: print(n) Output 1.35.56.58.9
One dimension array in Python
import array values = array.array(‘i’,[11,22,33,44,55,66]) print(‘Array: ‘,values) arr = array.array(‘i’) for i in range(6): val = int(input(‘Enter value: ‘)) arr.append(val) print(arr) Output Array: array(‘i’, [11, 22, 33, 44, 55, 66])Enter value: 1Enter value: 2Enter value: 3Enter value: 4Enter value: 5Enter value: 6array(‘i’, [1, 2, 3, 4, 5, 6])
Dataframe column values
import pandas as pd # Create data frame from csv filedf=pd.read_csv(“e://data/state-population.csv”) # Dataframe column values df[[‘year’, ‘population’]].head() # Dataframe column valuesdf.values Output array([[‘AL’, ‘under18’, 2012, 1117489.0], [‘AL’, ‘total’, 2012, 4817528.0], [‘AL’, ‘under18’, 2010, 1130966.0], …, [‘USA’, ‘total’, 2011, 311582564.0], [‘USA’, ‘under18’, 2012, 73708179.0], [‘USA’, ‘total’, 2012, 313873685.0]], dtype=object) year 0 2012 1117489.0 1 2012 4817528.0Continue reading “Dataframe column values”
Reverse characters
Program for reversing first three characters and next three characters in a word with even length. word = input(‘Enter word: ‘) if len(word)%2==0 and len(word) >= 6 : r1 = word[0:3] r2 = word[3:6] s1 = r1[::-1] s2 = r2[::-1] print(‘Word after reversing first 3 letters and next 3 letters:’,s1+s2) Output Enter word: PYTHON WordContinue reading “Reverse characters”
Alphanumeric
# Check type for alphanumeric input s = input(‘Enter alphanumeric string: ‘) if s.isalnum(): print(‘Input is alphanumeric. ‘) print(‘Data type: ‘,type(s)) else: print(‘Input is not alphanumeric.’) print(‘Your input: ‘,s) Output Enter alphanumeric string: a123a Input is alphanumeric. Data type: str Your input: a123a
Non-unique elements
numbers = [1,2,2,3,4,5,5,6,7,1,12,9,3] s = set() count = 0 for m in numbers: if numbers.count(m) > 1 : s.add(m) print(len(s)) Output: 4