Characters counting in text file

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 *

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”

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’]

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”

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