Skip to main content

Posts

Showing posts with the label Python

Py: 14. Python GUI

Python provides the standard library Tkinter for creating the graphical user interface for desktop-based applications. Developing desktop-based applications with python Tkinter is not a complex task. An empty Tkinter top-level window can be created by using the following steps. Exercises:-  Write a py program to create a window with a simple message. Write a py program to demonstrate Grid. Write a py program to generate a button. Write a py program to take user input in GUI format. Write a py program to perform arithmetic operation in GUI format.

Py: 13. Quiz Implementation

  Just looking for another "Real Life Problem" basic problem that can be automated and here I got an idea to make a basic quiz system, The program takes user choices and calculates the total based on the user inputs. Excited, Let's Start, Step 1:  Just Copy paste the code below in a file and save it with extension .py def quiz():     print("This is a simple gk quiz, Here you will get 4 marks for right answer and -1 mark for wrong answer.")     name=input("please enter your name: ")     print("*",name,"*welcome in the quiz:")     print("Q1. What is the height of Qutub minar?")     first_question=input("A. 82 m \nB. 73 m:\n")     if first_question=='B':         number=0         print('Correct answer')         number=(number+4)         print("YOUR SCORE: ",number)     else:         print('Wrong answer')         number=(number-1)         print("YOUR SCORE: ",number)     pri

Py: 12. Basic ATM Functionality

Just Giving it a try for the basic functionality of an ATM, Here I came up with some nasty bunch of code hope will help you in understanding the basic concept of programming in py. Step 1:  Just Copy paste the code below in a file and save it with extension .py def atm():     print("Welcome to HDFC Bank ATM");     pin= int(input("Enter 4 Digit PIN:"))     base_amount=10000;     choice=0;     update_amount=base_amount     while(choice!=3):         if(pin==1111):             print("Welcome Mohit");             if(choice==0):                 print("Your Account Balance is:",base_amount);             print("Enter Your Choice to continue:");             choice= int(input("1 For Deposit \n2 For Withdrawl \n3 For Exit:\n"))             if(choice==1):                 amount_to_deposit= int(input("Enter the amount to be deposited:"))                 update_amount=update_amount+amount_to_deposit;                 print("Am

Py: 11. Billing & Order Management For Restaurants

  Just looking for another "Real Life Problem" basic problem that can be automated and here I got an idea to make the order system for a Restaurant, The program takes customer choices and the quantity of each item and calculates the total based on the user inputs. Excited, Let's Start, Step 1:  Just Copy paste the code below in a file and save it with extension .py def restra():     print("WELCOME TO SANDOZ RESTRA")     total_bill=0     choice=0     quantity=0     pizza_total=0     burger_total=0     soft_drink_total=0          while(choice!=4):         print("\nEnter your choice to continue:")         choice= int(input("1. For Pizza Rs. 100\n2. For Burger Rs. 40\n3. For Soft Drinks Rs. 30\n4. For Exit:"))         if(choice==1):             quantity=int(input("You have selected Pizza Now Enter the quantity:"))             pizza_total=(pizza_total+100)*quantity             print("THE PIZZA TOTAL:",pizza_total)         if(ch

Py: 10. Python File Handling

Till now, we were taking the input from the console and writing it back to the console to interact with the user. Sometimes, it is not enough to only display the data on the console. It is impossible to recover the programmatically generated data again and again. The file handling plays an important role when the data needs to be stored permanently in the file. A file is a named location on a disk to store related information. We can access the stored information after the program termination. Exericses:- Write a py program to create a file. Write a py program to write some content in the file. Write a py program to read the data of a file. Write a py program to delete a file. Write a py program to demonstrate the append functionality in file handling. Write a py program to perform write and read operation in single file. Write a py program to create a CSV file. Write a py program to write data in a CSV file. Write a py program to read data from a CSV file. Write a py program to perfor

Py: 9. Python Exception Handling

 An exception can be defined as an unusual condition in a program resulting in the interruption in the flow of the program. Python provides a way to handle the exception so that the code can be executed without any interruption. Problem:-  a = int(input("Enter a:"))     b = int(input("Enter b:"))     c = a/b   print("a/b = %d" %c)          #other code:     print("Hi I am other part of the program")   Output:-  Enter a:10 Enter b:0 Traceback (most recent call last):   File "exception-test.py", line 3, in <module>     c = a/b; ZeroDivisionError: division by zero    Solution:-  try:       a = int(input("Enter a:"))         b = int(input("Enter b:"))         c = a/b   except:       print("Can't divide with zero")  #other code:     print("Hi I am other part of the program")   Output:- Enter a:10 Enter b:0 Can't divide with zero     Hi I am other part of the program Common Exceptions:- 

Py: 8. Python Dictionary

Python Dictionary is used to store the data in a key-value pair format, whereas Keys must be a single element Value can be any type such as list, tuple, integer, etc. Syntax:-  Dict = {"Name": "Tom", "Age": 22}     Example:-  Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}   print(type(Employee))   print("printing Employee data .... ")   print("Name :",Employee["Name"])   print("Age : ", Employee["Age"])   print("Salary :", Employee["salary"])   print("Company :", Employee["Company"])   Output:-  <class 'dict'> printing Employee data ....  Name : John Age :  29 Salary : 25000 Company : GOOGLE Exercises:- Write a py program to remove all items from the dicitionay Write a py program to copy one dictionary item to another Write a py program to print the value based on give

Py: 7. Python Tuple

 Tuple is a data structure in Python. It is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. The main difference between lists and tuples are : Lists are enclosed within brackets ( [ ] ), and their elements and size can be changed, while tuples are enclosed within parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists. Example:-  tuple1 = (10, 20, 30, 40, 50, 60)     for i in tuple1:         print(i)    Output:- 10 20 30 40 50 60 Exercises:- Write a py program to find the length of tuple Write a py program to find t he maximum element in tuple Write a py program to find t he minimum element in tupl e Write a py program to delete tuple Why Tuple:- Tuples use less memory whereas lists use more memory. We can use tuples in a dictionary as a key but it's not possible with lists. The tuple is immutable.

Py: 6. Python Lists

A list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created. The items in the list are separated with the comma (,) and enclosed with the square brackets []. Syntax:- L1 = ["Mohit", 102, "INDIA"]    List Indexing and Slicing The indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator []. The index starts from 0 and goes to length - 1.  Example:-  list = [1,2,3,4,5,6,7]   print(list[0])   print(list[1])   print(list[2])   print(list[3])   # Slicing the elements   print(list[0:6])   # By default the index value is 0 so its starts from the 0th element and go for index -1.   print(list[:])   print(list[2:5])   print(list[1:6:2])   Output:-  1 2 3 4 [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 7] [3, 4, 5] [2, 4, 6] Exercises:- Write a py program to find the length of the list Write a py program to find

Py: 5. Python Strings

Python string is the collection of the characters surrounded by single quotes, double quotes.  Python treated single quotes the same as double quotes. The sequence of characters may include a letter, a number, special character or a backslash. Syntax: str = "Hello World!"    Indexing   The indexing of the Python strings starts from 0 similar like list. Example:- str = "PYTHON"   print(str[0])   print(str[1])   print(str[2])   print(str[3])   print(str[4])   print(str[5])   print(str[6])   # Returns the IndexError because 7th index doesn't exist   print(str[7])   Output:-  P Y T H O N IndexError: string index out of range Exercise:-  Find length of a string in python Write a py program to convert string to capitalize. Write a py program to demonstrate split Method Write a py program to demonstrate replace Method.

Py: 4. Python Loops

The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times.  There are the following loop statements in Python. for loop The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary. Syntax:- for iterating_var in sequence:         statement(s)      Example: str = "Python"   for i in str:       print(i)   Output:-  P y t h o n For loop Using range() function The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9.  Syntax:- range(start,stop,step size)   Example: for i in range(10):       print(i,end = ' ')   Output:- 0 1 2 3 4 5 6 7 8 9  Exercises:- Write a py program to print all natural numbers from 1 to

Py: 3. Python Conditions

Decision making is the most important aspect of almost all the programming languages. Decision making allows us to run a particular block of code for a particular decision. In python, decision making is performed by the following statements. If statement The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed. Syntax:- if expression:       statement      Exercises:-  Write a py program to find maximum between two numbers. Write a py program to check whether a number is negative, positive or zero. If-else Statement It provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed. Syntax:- if condition:       #block of statements    else:        #another block of statements (else-block)    Exercises:- Write a py program to check whether a number is divisible by 5 and 11 or not. Write a py program to check

Py: 2. Python Fundamentals

 Variable:-  Variable is a name that is used to refer to memory location. In Python, we don't need to specify the type of variable because Python is smart enough to get variable type.  Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Syntax:-  num= 345 name= "mohit" Variable Types:- Number (Int, Float) Sting Boolean Get the Type of the variable:- x = 5 y = "John" print(type(x)) print(type(y)) Output:-  <class 'int'> <class 'str'> Operators:-  The operator can be defined as a symbol which is responsible for a particular operation between two operands. Python provides a variety of operators, which are described as follows. Arithmetic Operators:-  Assume variable a holds 10 and variable b holds 20, then − + Addition Adds values on either side of the operator. a + b = 30 - Subtraction Subtracts right hand operand from left hand operand. a – b = -10 * Multiplicat

Py: 1. Getting Started

Python is an open source, object oriented, high level programming language developed by Guido van Rossum in 1991 at the National Research Institute for Mathematics, Netherlands. Why Python? Shorter Code Object-Oriented Language Open Source Language GUI Programming Support Wide Range of Libraries and Frameworks Applications of Python Desktop Applications Web Applications Mobile Applications Artificial Intelligence Date Mining How to Use Use the following link to directly see it in action in your browser:- https://www.programiz.com/python-programming/online-compiler/ Or Download and install from the following link to Run it on your system. https://www.python.org/downloads/ First Program print("Hello World") Output:- Hello World  Separators:-  print(10,20,30,sep="*") print(10,20,30,sep=",") print(10,20,30,sep="\n") print(10,20,30,sep="\t") Output:-  10*20*30 10,20,30 10 20 30 10     20     30