Introduction - zamaniamin/python GitHub Wiki
Python
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
Python Syntax compared to other programming languages
Python was designed for readability, and has some similarities to the English language with influence from mathematics. Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.
Python Indentation
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. The number of spaces is up to you as a programmer, the most common use is four, but it has to be at least one. You have to use the same number of spaces in the same block of code, otherwise Python will give you an error.
if 5 > 2:
print("Five is greater than two!")
Variables
In Python, variables are created when you assign a value to it. Python has no command for declaring a variable. Variables do not need to be declared with any particular type, and can even change type after they have been set.
x = 5
y = "Hello, World!"
Comments
Python has commenting capability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment.
# print("Hello, World!")
print("Cheers, Mate!")
Multi Line Comments
Python does not really have a syntax for multi line comments. To add a multiline comment you could insert a # for each line:
# This is a comment
# written in
# more than just one line
print("Hello, World!")
# Or, not quite as intended, you can use a multiline string.
# Since Python will ignore string literals that are not assigned to a variable, you can add a
# multiline string (triple quotes) in your code, and place your comment inside it:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
In Python, everything is an object
In the Python programming language, everything can be regarded as an object comprising lists, integers, and functions. This feature can be verified using a Python interpreter as shown below:
def my_func():
return "guru99"
print("This is a function example and regarded as an object in Python:", isinstance(my_func, object))
# output: This is a function example and regarded as an object in Python: True
print("The following instance is an object:", isinstance(3, object))
# output: The following instance is an object: True
print("Another instance for object", isinstance(True, object))
# output: Another instance for object True
id
Python provides a built-in function named id that returns the object’s address as present in the memory of the Python programming language.