prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

Python Programming Part 05

Rajjit Sept 19th, 2023 5 mins read
part-05

TOPIC: Python Input Operation, Expressions in Python & Type Conversion

Input Operation
Using the input() function: The input() function is used to receive user input as a string. By default, the data-type for the input() function is a string data-type. Let us look at an example where the user is prompted to enter something and the input is stored as a string in the variable - user_input.

Example

user_input = input("Enter something: ")

It is used to prompt the users and instruct them on what to enter. The prompt inside the input() function is displayed to the user, indicating what kind of input is expected. We also need to handle the user input appropriately to ensure that our program behaves correctly. We can add conditions to check the user input and prevent errors.

Expressions in Python

  • Infix Expression: Infix expressions are the most common way of writing mathematical expressions that we use in our everyday mathematics. In these expressions, operators are placed between operands. Example of an infix expression: 3 + 4
  • Prefix Expression: They are also known as Polish Notation representing mathematical expressions where operators precede their operands. Example of a prefix expression: + 3 4
  • Postfix Expression: They are also known as Reverse Polish Notation (RPN), and represent mathematical expressions where operators follow their operands. Example of a postfix expression: 3 4 +
  • These expressions are not meant for normal uses like arithmetic calculations, but they will be used in some cases of data structures. So it will give errors if we try to use them in normal programs.

Type Conversion:

  • Implicit Type Conversion(Coercion): Python performs automatic type conversion when you perform operations between different data types. For example, when you add an integer and a float, Python converts the integer to a float before performing the addition. Example:
    x = 5 # integer
    y = 2.5 # float
    z = x + y # implicit conversion to float: 5 + 2.5 = 7.5
  • Explicit Type Conversion(Type Casting): We can manually convert data from one type to another using type casting functions such as int(), float(), str(), etc. Example:
    x = "123" # string
    y = int(x) # explicit conversion to integer
  • We can convert among the various data-types using int() for integers, float() for floating point numbers, str() for string, etc.
  • Type conversion is a fundamental concept in Python that allows you to work with different data types and ensure that your program behaves as expected when manipulating and interacting with data.
Video tutorial:

Code as discussed in the video:

# Python Input Operations

# inputVar = input("Enter your name: ") # string by default
# print("My name is ", inputVar)
# inputVarI = int(input("Enter your age: "))
# print("My age is ", inputVarI)

# floatVar = float(input("Enter any floating number: "))
# print(floatVar)

# Python Expressions
# 1. Infix # 2. Prefix # 3. Postfix
# num1 = 5
# num2 = 4

# infix
# num3 = num1 - num2 # the operator is between the operands
# print(num3)
# prefix
# num4 = *num1 num2 # the operator is before the operands
# postfix
# num5 = num1 num2* # the operator is after the operands

# Type Conversion
# num1 = float(input("Enter a number: "))
# num2 = float(input("Enter another number: "))
# num3 = num1+num2
# print(int(num3))
'''
Q.1 Write a python program to calculate the area of an rectangle and also its perimeter
- accept values from the user
- use float type for the length and the breadth
- store the value in area, peri and print it

Q.2 Write a python program to calculate the area of a circle and also its circumference
- pi = 3.14
- accept values from the user
- use float type for the radius
- store the value in carea, circum
'''

You can copy and run this code