Reference code snippets for the Python programming language.
1 Check Python installed Version
> python --version
Python 3.9.7
2 Hello World Program
Filename: firstpythoncode.py
print("Hello, World!")
Run the programin from terminal
>python firstpythoncode.py
3 Comments
Single line comments
#Print Hello Python on Console
print("Hello Python")
Multiline comments
#Below code displays
#Hello World
print("Hello, World!")
"""
Below code displays
Hello World
"""
print("Hello, World!")
Python will ignore string literals if no assingnment used.
msg = """
Below code displays
Hello World
"""
print(msg)
Output
Below code displays
Hello World
4 Variables
a = 100
name = "abhi"
type() funciton
print(type(a)) #<class 'int'>
print(type(name)) #<class 'str'>
Ilegal variable names
2varname = "Abhi"
var-name = "Abhi"
var name = "Abhi"
#SyntaxError: invalid syntax
Legal Variable Names
var_name = "Abhi"
varName = "Abhi"
_var_name = "Abhi"
VARNAME = "Abhi"
varname1 = "Abhi"
5 Keywords
Keywords words can't be used as variable and function names.
Program to print all keywords
# importing the module
import keyword
print(keyword.kwlist)
Output
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
and = "Hello"
#SyntaxError: invalid syntax
6 Identifiers
Todo
7 Constants
Todo