Hello friends, data entry in Python programming language is done with input() function. With this function, when we receive data from the user, the data we receive must be assigned to a variable.
There is a way to use Input(prompt). Here, the prompt feature is optional.
The input function can be used to receive textual and numerical data from the user.
When using the input function, we need to assign a variable. In our first example, we assign the variable named name.
name = input("Please input your name: ")#asks for an input.
print() #outputs a blank line to help with layout.
print("Hello", name, end="!\n")#joins "Hello" and their name together.
Figure 1
In our second example, we assign the two numbers we entered in the input to the variables number1,number2. We print the sum of these two variables.
print(" ***** Toplama Islemi ***** ")
print(" ***** Addition... Enter any two numbers ***** ")
print("-"*25)
number1=int(input("Enter the first number (1.Sayiyi giriniz:)"))
number2=int(input("Enter the second number (2.Sayiyi giriniz:)"))
total=number1+number2
print("-"*23)
print("Result-Islem Sonucu:",number1,"+",number2,"=",total)
Figure 2
In our third example, we will see the use of the input function without using a prompt.
i = input()
print('You entered (girdiginiz deger) : ', i)
Figure 3
In our last example, we will enter two variables with the input function and add these entered variables to each other.
Figure 4
# taking val1 input from user as list
val1 = list(input(" Please enter elements of val1 \n val1 degerlerini giriniz: "))
# taking val2 input from user as list
val2 = list(input(" Please enter elements of val2 \n val2 degerlerini giriniz: "))
# appending val2 into val1 using append function
for i in val2:
val1.append(i)
# printing val1.
print(val1)
We have come to the end of this article. See you in our next article. Happy coding. Bahadir Sahin
No comments:
Post a Comment