See also: https://realpython.com/learn/python-first-steps/
http://www.hlevkin.com/Shell_progr/hellopython.htm
http://sthurlow.com/python/lesson10/ (for file I/O)
1 2 3 4 5 6 | # Python program Filereadchars.py# Open a file text_file = open("C:\mystuff\Ace\python.txt", "r+") print text_file.readline(1) print text_file.readline(5) text_file.close() |
1 2 3 4 5 6 | # Python program Filereadwhole.py# Open a file text_file = open("C:\mystuff\Ace\python.txt", "r+") whole_thing = text_file.read() print whole_thing text_file.close() |
1 2 3 4 5 6 7 8 | # Python program Fileread.py# Open a file # text_file = open(C:\Users\ASwarup\Documents\Python-Examples\python.txt","r+") text_file = open("C:\mystuff\Ace\python_ex.txt","r+") lines = text_file.readlines() print "Read String is : ", lines # Close opened file text_file.close() |
# Ex01_remove_duplicates.py: Program to remove duplicates from a list data=[] for i in range(6): item = int(input("Enter next number ")) data.append(item) # data = [10,20,30,20,10,50,60,40,80,50,40] dup_items = set() uniq_items = [] for x in data: if x not in dup_items: uniq_items.append(x) dup_items.add(x) #print(dup_items) print(uniq_items) uniq_items.sort() print(uniq_items)
# Ex02_sum.py: Python program to sum all the items in a list def sum_list(items): sum_numbers = 0 for x in items: sum_numbers += x return sum_numbers data=[] for i in range(6): item = int(input("Enter next number ")) data.append(item) # data = [10,20,30,20,10,50,60,40,80,50,40] # print(sum_list([1,2,-8])) # print(sum_list(data)) # http://www.thomas-cokelaer.info/tutorials/python/print.html print("Sum of entered numbers is = %s" % sum_list(data))
# Ex03_Clone_List.py: Program to clone or copy a list # original_list = [10, 22, 44, 23, 4] list1=[] for i in range(5): item = int(input("Enter next number ")) list1.append(item) new_list = list(list1) print("Original list is: ") print(list1) print("Copied list is: ") print(new_list)
# Ex04_Chk_List_Empty1.py: Program to check if a list is empty or not mylist=[] i=0 while 1: i+=1 item=raw_input('Enter item %d: '%i) if item=='': break mylist.append(item) print(mylist) if not mylist: print("List is empty") else: print("List is not empty")
# Ex04b_Chk_List_Empty.py: Program to print data from a list and message when list empty # list1 = [10, 22, 44, 23, 4] list1=[] for i in range(5): item = int(input("Enter next number ")) list1.append(item) print list1 for i in range(5): list1.pop() print list1 if not list1: print("List is empty.")
# Ex05_Compare_Lists_2.py: A python program that takes two lists and returns True ... # ... if they have at least one common member # See also: http://www.w3resource.com/python-exercises/ data = [] for i in range(5): card = int(input("Enetr the next item ")) data.append(card) print data packet = [] for i in range(5): card = int(input("Enetr the next item ")) packet.append(card) print packet def common_data(list1, list2): result = False for x in list1: for y in list2: if x == y: result = True return (str(result)) # print(common_data([1,2,3,4,5], [5,6,7,8,9])) # print(common_data([1,2,3,4,5], [6,7,8,9]) print(common_data(data, packet))
# Ex06_Number_of_evens_odds.py: Count the number of evens & odds from a series of numbers numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declare the tuple count_odd = 0 count_even = 0 for x in numbers: if not x % 2: count_even+=1 else: count_odd+=1 print("Number of even numbers :",count_even) print("Number of odd numbers :",count_odd)
# Ex07_Insert_in_a_List.py: Program to insert number in a list in a sorted order # Note: An extra indent couldn't be seen in notepad++, but showed in keditw import bisect def main(): elements = raw_input("Enter list of numbers separated by spaces: ").split(' ') numberList = list(set(elements)) numbers = sorted(map(int, numberList)) print("The distinct numbers are: ", numbers) ilist = [] prompt = "Number to insert -> " nmbr = raw_input(prompt) while nmbr: num = int(nmbr) # print('Number %s \n' % num) ilist.append(int(nmbr)) bisect.insort_left(numbers, num) nmbr = raw_input(prompt) # print(ilist) print("The modified list is: ", numbers) main()
# Ex08_Number_of_Upper-Lower_Chars.py: Count the number of uppercase and lowercase letters in a string def string_test(s): d={"UPPER_CASE":0, "LOWER_CASE":0} for c in s: if c.isupper(): d["UPPER_CASE"]+=1 elif c.islower(): d["LOWER_CASE"]+=1 else: pass print ("Original String : ", s) print ("No. of Upper case characters : ", d["UPPER_CASE"]) print ("No. of Lower case Characters : ", d["LOWER_CASE"]) # string_test('The quick Brow Fox') cals = raw_input("Enter the String ") string_test(cals)
# Ex09_Check_Palindrome.py: Program to check whether a passed string is palindrome or not def isPalindrome(string): left_pos = 0 right_pos = len(string) - 1 while right_pos >= left_pos: if not string[left_pos] == string[right_pos]: return False left_pos += 1 right_pos -= 1 return True print(isPalindrome('aza')) print(isPalindrome('madam')) print(isPalindrome('nurses run'))
# Ex10_Check_if_number_in_range.py: Function to check if a passed number ... # ... is in a pre-specified range (3, 99) def test_range(n): if n in range(3,99): print("The number %s is in the range" % str(n)) else: print("The number %s is outside the given range" % str(n)) test_range(15) test_range(101)