By reading this article your following problems will be answered.
How to manipulate text and numbers in Python.
How to print text and numbers in Python.
How to use Strings in Python.
How to do calculations in Python.
Special operators in Python.
Basic text in Python
- To print on python,
print(“Text”)
- In python a text should always be a “String”, and it should be inside “”.
- To combine two text strings together, a ‘+’ sign is always required. And the two strings need to be always inside “”.
print(“Hello ” + “Python”)
- Python does not support ‘&’ when combining two text strings. (Only ‘+’ is valid.)
- Alternatively a comma also can be used to combine two strings.
- But when using a comma, it will automatically add a space between those two strings.
- Adding a space manually before or after any string is not required with comma.
print(“Hello”, “Python”)
Numbers in Python
- When we need to display numbers in python, numbers can be interpreted without “” as just numbers.
print(3)
- Numbers can be calculated in print function (Using +, -, / & *)
print(1 + 2)
- Python uses BODMAS to calculate numbers.
- When combining a text string and a number (Not a string, just a plain number) in python, a comma is always the right thing. In this case adding ‘+’ between a test string and a number (Without “”) gives an error.
- But if we consider the number as a string by adding “”, ‘+’ will definitely work.
Special operators in Python
- When // is added between two numbers it rounds down the division to a whole number.
- Actually, when // is used it gives the largest integer as the output.
print(9//3) [E.g.: Output is 3]
print(10//3)
print(16//3)
print(100//6)
- When % added between two numbers it displays the remainder.
print(4%2) [E.g.: Output is 0]
print(3%2)
print(10%6)
- The operators // and % works similar to division but they gives different outputs.