Hello Earth
Stringβ
Strings in python are the collection of characters. The characters in the string are declared in single or double-quotes.
message = "Let the earth breathe"
Printing a statementβ
To display Hello Earth in the Python shell:
>>> print("Hello Earth")
To display a Hello Earth statement in a python script:
print("Hello Earth")
Variablesβ
Variables are like containers. They either hold values or strings. Python is dynamically-typed which means the programmer doesnβt have to declare the datatype of the variable. The Python interpreter will automatically know the type of the variable.
To declare a variable named x:
>>> x = 2
note
{" "} Python has a built in method to check the datatype of a variable. The method is called type(name_of_variable)
To check the datatype of x:
>>> type(x)
<class 'int'>
To declare a string:
>>> message = "Humans are smart"
We can check the datatype of message:
>>> type(message)
<class 'str'>
New lineβ
To print a output in multiple lines, use \n:
>>> print("Earth is not green anymore\n", "Is this true?\n")
Earth is not green anymore
Is this true?
Listβ
Python provides built-in lists and the basic one is a sequence. Each element in the sequence has its index. Modifying elements of a list is possible. To define a list:
names = ["Bill", "Steve"]
print(names)
Output:
['Bill', 'Steve']
Tuplesβ
Tuples are objects similar to the list. Unlike the list, it is immutable and therefore cannot be changed once created. There are multiple ways to create tuples. Tuple allows you to access, and append the tuples. Modifying or updating an existing tuple is not possible. You can create a new tuple and delete the old one. The index for tuple starts with β0β, which is similar to the list.
companies = ("Microsoft", "Apple")
print(companies)
Output:
('Microsoft', 'Apple')
Dictionariesβ
In Python, dictionary is more of structured list. To define a dictionary:
information = {"Name": "Bruce", "Company": "Wayne Industries"}
print(information["Name"])
Output:
Bruce