Learning Python from Basics
Language, Interpreter and IDE • The word “Python” to refer to the language, and the word “interpreter” to refer to the technology that runs your Python code. • “IDLE” refers to the IDE, which takes your Python code and runs it through the interpreter. • It’s the interpreter that does all the actual work here.
Standard library • The first line of code imports some preexisting functionality from Python’s standard library, which is a large stock of software modules providing lots of prebuilt (and high-quality) reusable code.
Standard libraries >>import sys >>sys.platform • 'win32’ >>print(sys.version) • 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] To determine the identity of your underlying operating system To determine which version of Python is running
>>import os >>os.getcwd() • 'C:UsersBagavathiAppDataLocalPrograms PythonPython311’ >>os.getenv('HOME') • 'C:UsersBagavathi' to work out the name of the folder your code is operating within to get the environment HOME
• os.environ • environ({'ALLUSERSPROFILE': 'C:ProgramData', 'APPDATA': 'C:UsersBagavathiAppDataRoaming', 'COMMONPROGRAMFILES': 'C:Program FilesCommon Files', 'COMMONPROGRAMFILES(X86)': 'C:Program Files (x86)Common Files', 'COMMONPROGRAMW6432': 'C:Program FilesCommon Files', 'COMPUTERNAME': 'BAGAVATHI-SHIVA', 'COMSPEC': 'C: WINDOWSsystem32cmd.exe', 'DRIVERDATA': 'C:WindowsSystem32DriversDriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'HOME': 'C:Users Bagavathi', 'HOMEDRIVE': 'C:', 'HOMEPATH': 'UsersBagavathi', 'LOCALAPPDATA': 'C:UsersBagavathiAppDataLocal', 'LOGONSERVER': 'BAGAVATHI-SHIVA', 'NUMBER_OF_PROCESSORS': '4', 'ONEDRIVE': 'C:UsersBagavathiOneDrive - Amrita Vishwa Vidyapeetham', 'ONEDRIVECOMMERCIAL': 'C:UsersBagavathiOneDrive - Amrita Vishwa Vidyapeetham', 'ONEDRIVECONSUMER': 'C:UsersBagavathiOneDrive', 'ONLINESERVICES': 'Online Services', 'OS': 'Windows_NT', 'PATH': 'C: Program FilesCommon FilesOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32 Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WINDOWSSystem32OpenSSH;C:Program Files MATLABR2022aruntimewin64;C:Program FilesMATLABR2022abin;C:Program Files (x86)dotnet;C:Program FilesGitcmd;C:UsersBagavathiAppDataLocalMicrosoftWindowsApps;C:modeltech64_10.1cwin64;C:Users BagavathiAppDataLocalProgramsMiKTeXmiktexbinx64;C:Program FilesJetBrainsPyCharm 2022.2.3bin;;;C: Program FilesJetBrainsPyCharm Community Edition 2022.3bin;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PLATFORMCODE': 'KV', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'AMD64 Family 23 Model 24 Stepping 1, AuthenticAMD', 'PROCESSOR_LEVEL': '23', 'PROCESSOR_REVISION': '1801', 'PROGRAMDATA': 'C:ProgramData', 'PROGRAMFILES': 'C:Program Files', 'PROGRAMFILES(X86)': 'C:Program Files (x86)', 'PROGRAMW6432': 'C:Program Files', 'PSMODULEPATH': 'C:Program FilesWindowsPowerShellModules;C:WINDOWS system32WindowsPowerShellv1.0Modules', 'PT8HOME': 'C:Program FilesCisco Packet Tracer 8.1.1', 'PUBLIC': 'C:Users Public', 'PYCHARM': 'C:Program FilesJetBrainsPyCharm 2022.2.3bin;', 'PYCHARM COMMUNITY EDITION': 'C:Program FilesJetBrainsPyCharm Community Edition 2022.3bin;', 'PYTHONPATH': 'F:ToolsVSCode-win32-x64-1.70.2resourcesapp extensionspython', 'REGIONCODE': 'APJ', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:WINDOWS', 'TEMP': 'C:UsersBAGAVA~1AppDataLocalTemp', 'TMP': 'C:UsersBAGAVA~1AppDataLocalTemp', 'USERDOMAIN': 'BAGAVATHI-SHIVA', 'USERDOMAIN_ROAMINGPROFILE': 'BAGAVATHI-SHIVA', 'USERNAME': 'Bagavathi', 'USERPROFILE': 'C: UsersBagavathi', 'WINDIR': 'C:WINDOWS'})
>>import datetime >>datetime.date.today().day • 31 >> datetime.date.today().month • 1 >> datetime.date.today().year • 2023 >>datetime.date.isoformat(datetime.date.today()) • '2023-01-31' To get today’s date, To pass in today’s date to display a much more userfriendly version of today’s date
>>import time >>time.strftime("%H:%M") • '13:31’ >>time.strftime("%A %p") • 'Tuesday PM' To specify how you want the time displayed To specify the day and the time currently as PM or AM
>>import html >>html.escape("This HTML fragment contains a <script>script</script> tag.") • 'This HTML fragment contains a &lt;script&gt;script&lt;/script&gt; tag.' To convert to HTML
Data structures • One of the data structures is the list, which can be thought of as a very powerful array. Like arrays in many other languages, lists in Python are enclosed within square brackets ([]). • Interpreter won’t decide a single statement has come to an end until it finds the closing bracket (]) that matches the opening one ([). • The end of the line marks the end of a statement in Python.

Introduction to Basics of python language

  • 1.
  • 2.
    Language, Interpreter andIDE • The word “Python” to refer to the language, and the word “interpreter” to refer to the technology that runs your Python code. • “IDLE” refers to the IDE, which takes your Python code and runs it through the interpreter. • It’s the interpreter that does all the actual work here.
  • 3.
    Standard library • Thefirst line of code imports some preexisting functionality from Python’s standard library, which is a large stock of software modules providing lots of prebuilt (and high-quality) reusable code.
  • 4.
    Standard libraries >>import sys >>sys.platform •'win32’ >>print(sys.version) • 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] To determine the identity of your underlying operating system To determine which version of Python is running
  • 5.
    >>import os >>os.getcwd() • 'C:UsersBagavathiAppDataLocalPrograms PythonPython311’ >>os.getenv('HOME') •'C:UsersBagavathi' to work out the name of the folder your code is operating within to get the environment HOME
  • 6.
    • os.environ • environ({'ALLUSERSPROFILE':'C:ProgramData', 'APPDATA': 'C:UsersBagavathiAppDataRoaming', 'COMMONPROGRAMFILES': 'C:Program FilesCommon Files', 'COMMONPROGRAMFILES(X86)': 'C:Program Files (x86)Common Files', 'COMMONPROGRAMW6432': 'C:Program FilesCommon Files', 'COMPUTERNAME': 'BAGAVATHI-SHIVA', 'COMSPEC': 'C: WINDOWSsystem32cmd.exe', 'DRIVERDATA': 'C:WindowsSystem32DriversDriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'HOME': 'C:Users Bagavathi', 'HOMEDRIVE': 'C:', 'HOMEPATH': 'UsersBagavathi', 'LOCALAPPDATA': 'C:UsersBagavathiAppDataLocal', 'LOGONSERVER': 'BAGAVATHI-SHIVA', 'NUMBER_OF_PROCESSORS': '4', 'ONEDRIVE': 'C:UsersBagavathiOneDrive - Amrita Vishwa Vidyapeetham', 'ONEDRIVECOMMERCIAL': 'C:UsersBagavathiOneDrive - Amrita Vishwa Vidyapeetham', 'ONEDRIVECONSUMER': 'C:UsersBagavathiOneDrive', 'ONLINESERVICES': 'Online Services', 'OS': 'Windows_NT', 'PATH': 'C: Program FilesCommon FilesOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32 Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WINDOWSSystem32OpenSSH;C:Program Files MATLABR2022aruntimewin64;C:Program FilesMATLABR2022abin;C:Program Files (x86)dotnet;C:Program FilesGitcmd;C:UsersBagavathiAppDataLocalMicrosoftWindowsApps;C:modeltech64_10.1cwin64;C:Users BagavathiAppDataLocalProgramsMiKTeXmiktexbinx64;C:Program FilesJetBrainsPyCharm 2022.2.3bin;;;C: Program FilesJetBrainsPyCharm Community Edition 2022.3bin;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PLATFORMCODE': 'KV', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'AMD64 Family 23 Model 24 Stepping 1, AuthenticAMD', 'PROCESSOR_LEVEL': '23', 'PROCESSOR_REVISION': '1801', 'PROGRAMDATA': 'C:ProgramData', 'PROGRAMFILES': 'C:Program Files', 'PROGRAMFILES(X86)': 'C:Program Files (x86)', 'PROGRAMW6432': 'C:Program Files', 'PSMODULEPATH': 'C:Program FilesWindowsPowerShellModules;C:WINDOWS system32WindowsPowerShellv1.0Modules', 'PT8HOME': 'C:Program FilesCisco Packet Tracer 8.1.1', 'PUBLIC': 'C:Users Public', 'PYCHARM': 'C:Program FilesJetBrainsPyCharm 2022.2.3bin;', 'PYCHARM COMMUNITY EDITION': 'C:Program FilesJetBrainsPyCharm Community Edition 2022.3bin;', 'PYTHONPATH': 'F:ToolsVSCode-win32-x64-1.70.2resourcesapp extensionspython', 'REGIONCODE': 'APJ', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:WINDOWS', 'TEMP': 'C:UsersBAGAVA~1AppDataLocalTemp', 'TMP': 'C:UsersBAGAVA~1AppDataLocalTemp', 'USERDOMAIN': 'BAGAVATHI-SHIVA', 'USERDOMAIN_ROAMINGPROFILE': 'BAGAVATHI-SHIVA', 'USERNAME': 'Bagavathi', 'USERPROFILE': 'C: UsersBagavathi', 'WINDIR': 'C:WINDOWS'})
  • 7.
    >>import datetime >>datetime.date.today().day • 31 >>datetime.date.today().month • 1 >> datetime.date.today().year • 2023 >>datetime.date.isoformat(datetime.date.today()) • '2023-01-31' To get today’s date, To pass in today’s date to display a much more userfriendly version of today’s date
  • 8.
    >>import time >>time.strftime("%H:%M") • '13:31’ >>time.strftime("%A%p") • 'Tuesday PM' To specify how you want the time displayed To specify the day and the time currently as PM or AM
  • 9.
    >>import html >>html.escape("This HTMLfragment contains a <script>script</script> tag.") • 'This HTML fragment contains a &lt;script&gt;script&lt;/script&gt; tag.' To convert to HTML
  • 10.
    Data structures • Oneof the data structures is the list, which can be thought of as a very powerful array. Like arrays in many other languages, lists in Python are enclosed within square brackets ([]). • Interpreter won’t decide a single statement has come to an end until it finds the closing bracket (]) that matches the opening one ([). • The end of the line marks the end of a statement in Python.