Posted in C++/Python

Operators

  1. Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.
OPERATORDESCRIPTIONSYNTAX
+Addition: adds two operandsx + y
Subtraction: subtracts two operandsx – y
*Multiplication: multiplies two operandsx * y
/Division (float): divides the first operand by the secondx / y
//Division (floor): divides the first operand by the secondx // y
%Modulus: returns the remainder when first operand is divided by the secondx % y
**Power : Returns first raised to power secondx ** y

Example:

for example, open any of your text editor(notepad/notepad++), then copy the code below

a = 9
b = 4
#Addition of numbers
add = a + b 
#Subtraction of numbers
sub = a - b 
#Multiplication of number
mul = a * b 
#Division(float) of number
div1 = a / b 
#Division(floor) of number
div2 = a // b 
#Modulo of both number
mod = a % b 
#Power
p = a ** b 
print results
print(add) 
print(sub) 
print(mul) 
print(div1) 
print(div2) 
print(mod) 
print(p) 

Save this file with test.py, then search your Command Prompt (.cmd) in your windows, open it, then type: python test.py, or python3 test.py.

Output:

13 5 36 2.25 2 1 6561
Advertisement
Posted in Home

Input/Output

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard.

  • input ( type your input here)
  • raw_input ( type your input here )

input ( ) : This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by python. For example:

  • Các nhà phát triển thường có nhu cầu tương tác với người dùng, để lấy dữ liệu hoặc cung cấp một số loại kết quả. Hầu hết các chương trình ngày nay đều sử dụng hộp thoại như một cách để yêu cầu người dùng cung cấp một số kiểu đầu vào.
  • Trong khi Python cung cấp cho chúng ta hai hàm có sẵn để đọc đầu vào từ bàn phím.
    • input (lời nhắc)
    • raw_input (lời nhắc) input ():
  • Hàm này trước tiên lấy đầu vào từ người dùng và sau đó đánh giá biểu thức, có nghĩa là Python tự động xác định xem người dùng đã nhập một chuỗi hay một số hoặc danh sách. Nếu đầu vào được cung cấp không chính xác thì một trong hai lỗi cú pháp hoặc ngoại lệ được đưa ra bởi python. Ví dụ :
Python program showing
 a use of input()
 val = input("Enter your value: ") 
 print(val)