The purpose of these tutorial is to quickly go thru the basics of Python. Some of the Python's characteristics:
$ python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Dec 13 2019, 18:00:32)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
3.X Version with upgrade in Mac:
$ python3
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
print('DbmsTutorials')
Output: DbmsTutorials
******************firstFileCode.py Content***************
print('Python: First File Code')
*********************************************************
Running Code File:
python /path_to_the_file/firstFileCode.py
Output: Python: First File Code
Running Code File in upgraded Mac:
python3 /path_to_the_file/firstFileCode.py
Output: Python: First File Code
>>> help
Type help() for interactive help, or help(object) for help about object.
Example 1: Help of variable name will give functions & operation that can be performed on this variable.
>>> a=3
>>> help(a)
Output:
Help on int object:
class int(object)
| int(x=0) -> int or long
| int(x, base=10) -> int or long
Example 2: Get the syntax & usage of 'print' function.
>>> help('print')
Output:
The "print" statement
*********************
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
Example 2.1: Below statement will return operator precedence from lowest precedence to highest precedence.
>>> help('EXPRESSIONS')
Example 3: This will start interactive help console
>>> help()
Output:
Welcome to Python 3.8''s help utility!
help>
Example 3.1:
help> modules
Output:
Please wait a moment while I gather a list of all available modules...
AVFoundation _bisect dl pstats
Accounts _builtinSuites doctest pty
AddressBook _cffi_backend dumbdbm pwd
AppKit _codecs dummy_thread py2app
.
.
Example 3.2:
help> modules os
Here is a list of matching modules. Enter any module name to get more help.
posix - This module provides access to operating system functionality that is
_osx_support - Shared OS X support functions.
binhex - Macintosh binhex compression/decompression.
.
.
str_ex = 'This is Python\'s example' # \ is escaping single quote
print(str_ex)
Output:
This is Python's example
str_ex = 'This \\n (slash + n) will be displayed as string'
print(str_ex)
Output:
This \n (slash + n) will be displayed as string
Example 1: Comment whole line
print('Before Comment')
#print('This code is commented, will not print')
print('After Comment')
Output:
Before Comment
After Comment
Example 2: Comment description of code at the end of line
print('Before Comment') #This code will print.
Output: Before Comment
Example 1: Using triple quotes as multiline comment
print('This is before Multiline Comment')
'''
Triple quote can be used
As
a
Multiline Comment
'''
print('This is after Multiline Comment')
Output:
This is before Multiline Comment
This is after Multiline Comment
Example 2: Python recommend to use hash(#) in front of every line.
print('This is before Multiline Comment')
# Hash against each line
# As
# a
# Multiline Comment
print('This is after Multiline Comment')
Output:
This is before Multiline Comment
This is after Multiline Comment
Example 1: Press CTRL + D
>>> ^D
Example 2: Type exit() and press enter
>>> exit()