Before we explore functions deeper, I want you guys to be clear on Python’s different data types. Let’s get started without blah blah!
Hey, stop. Read previous posts if you haven’t read yet! “Here“
Data types are not at all a big topic in Python. You just have to know Strings and Numbers in Python!
Strings in Python:
A string is simply a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings like this:
'This is a string' "This is also a string"
Can I single quote or double quote string in Python?
This is for your versatility to use quotes or apostrophes inside your string like this:
“I’d like to hear from you.”
‘My friend said, “He Loves Python.” ‘
You can use quotes inside your string as above!
Concatenating Strings in Python:
String concatenation is often useful to a programmer. For example, if you’re getting the first name in one variable and the last name in another variable and you want to show it together as a full name, you can concatenate the strings. See the below example:
first_name = "Narendran" last_name = "Kannan" print("My full name is " + first_name + " " + last_name) # Output: My full name is Narendran Kannan
The above code will print My full name is Narendran Kannan.
Tabs and new lines in Python print:
In programming, whitespace refers to any nonprinting character, such as spaces, tabs, and end-of-line symbols. You can use whitespace to organize your output, so it’s easier for users to read.
Tab: To add a tab in your string, use \t to leave some space before it. See the example below
print(“I love \tPython”)
I love Python
It added a tab before “Python.”
NewLines: To add a new line in a string, use the character combination \n like this:
print("I\nLove\nPython") I Love Python
Stripping whitespace in Python:
Whitespace may be confusing and cause difficulty in reading in some cases. To avoid this, there is a method “strip()” in Python to remove whitespaces in the left and right side of the string. See the example,
string = ” I love Python”
I love Python
Numbers in Python:
Numbers are the important datatype in all programming languages to keep score in games, represent data in visualizations, store information in web applications, and so on. Python treats numbers in different ways like Integer, Float depending on how they are being used.
Integers in Python:
Integers are just like the numbers without a fractional component. You can add (+), sub (-), divide (/), multiply (*), mod (%), exponents (**) with integers in Python.
>>> 3 + 3
>>> 3 – 3
>>> 2 * 3
>>> 3 / 2
>>> 3 ** 2
>>> 10 ** 5
(Ignore >>> symbols, it’s just console symbols)
Floats in Python:
In Python, a float is a number with a decimal point. You can use decimals in Python number as below:
>>> 0.2 + 0.1
>>> 0.3 + 0.3
>>> 2 * 0.2
(Note: Python2 numbers are slightly different than Python3.)
Comments in Python:
Comments are extremely useful in programming. Comment lines won’t get executed or run in your program, and we use it to write notes about your program. Python comment starts with “#” (asterisk) symbol. See the below example:
#The below line will say "Hello World" print("Hello World")
Whenever you or your colleague see this code, you’ll get to know for what purpose you’ve coded this block. Do you agree comments are useful?
Try today’s code by using the above button!
I hope you’re clear in python data types and comments!
See you in the next tutorial! Cheers ❤️