Python Example

 Interactive interpreter prompt

hello world
 print ("hello world");  -------> click enter



Using a script file (Script Mode Programming)

Open Python
New file
Type : print("hello world");
Save with .py extension
Run --->>f5

Output: will appear on Python Console


we can also run the file using the operating system terminal. But, we should be aware of the path of the directory where we have saved our file.

  • Open the command line prompt and navigate to the directory.


============================================================================================

Multiline Program



name="hari taraka prabhu"
age="31"
branch="computer science"

print("My name is: ", name, )
print("---------------------------------------------------------------------")

print("My age is: ", age)
print("---------------------------------------------------------------------")

print(name,"age is", age)
print("---------------------------------------------------------------------")

print ("my name is:",name, "and my age is:",age,", and iam studing in: ",branch)
print("---------------------------------------------------------------------")


OUTPUT:

========= RESTART: C:\Users\mhtpr\Documents\FormatFactory\multi_line.py ========

My name is:  hari taraka prabhu
---------------------------------------------------------------------
My age is:  31
---------------------------------------------------------------------
hari taraka prabhu age is 31
---------------------------------------------------------------------
my name is: hari taraka prabhu and my age is: 31 , and iam studing in:  computer science
---------------------------------------------------------------------





============================================================================================

In Python, statements that are the same level to the right belong to the same block. We can use four whitespaces to define indentation. Let's see the following lines of code.

Loop Block

list1 = [1, 2, 3, 4, 5]  

for i in list1:  

    print(i)  

    if i==4:  

       break  

print("End of for loop")  


Output:

========= RESTART: C:/Users/mhtpr/Documents/FormatFactory/loop_block.py ============================

1

2

3

4

End of for loop

============================================================================================

Comments Program in Python

name  = "Thomas"   # Assigning string value to the name variable

Fees = 10000      # defining course fees is 10000  
Fees = 20000      # defining course fees is 20000

print (name,Fees)
print ("===============================")



#Single-Line Comment - Single-Line comment starts with the hash # character followed by text for further explanation.


Marks = 90 # defining the marks of a student   
Name = "James"   # the name of a student is James  
Branch = "Computer Science"   # defining student branch

print (Marks, Name, Branch)


#Multi-Line Comments - Python doesn't have explicit support for multi-line comments but we can use hash # character to the multiple lines. For example -


OUTPUT

============================================================== RESTART: C:/Users/mhtpr/Documents/FormatFactory/comments.py =============================================================
Thomas 20000
===============================
90 James Computer Science



============================================================================================

Python Identifiers

Python identifiers refer to a name used to identify a variable, function, module, class, module or other objects. There are few rules to follow while naming the Python Variable.

  • A variable name must start with either an English letter or underscore (_).
  • A variable name cannot start with the number.
  • Special characters are not allowed in the variable name.
  • The variable's name is case sensitive.

Example:

number = 10  
print(number)  
  
_a = 100  
print(_a)  
  
x_y = 1000  
print(x_y)  


Output:

10
100
1000