echo '' ;

Python – Examples

Here you can find more examples codes of python.

Find out the Platform type

In this example you will get to know about how to know the plat form type running.

You should have the knowledge of following

  • import module: Import in python is similar to #include header_file in C/C++
  • platform module and platform.system().lower() function
  • if else loop
  • print function
  • variable declaration in python
  • # command line argument
  • = assignment operator
  • == comparison operator
# "Platform_Type" variable hold the platform type

#print the explame code platform type in console
print("ArunEworld: Find out the platform type example")

#Need for identify the platform type like windows or others using platform.system().lower()
import platform 

#set the variable with host name (Enter the host here)
hostname = "www.aruneworld.com"

#This if, else loop will find the platform type
if platform.system().lower() == "windows":
    Platform_Type = "ping -n 1 "
else:
    Platform_Type = "ping -c 1 "

#print the platform type in console
print("Ping is running in ",platform.system().lower(), " Platform")

##Output
#ArunEworld: Find the running platform type example
#Running in  windows  Platform

Ping to host

In this example you will get to know about how to write the ping code as well as how to know the plat form type running.

for this example you should have the knowledge of following

  • import module: Import in python is similar to #include header_file in C/C++
  • os module and os.system() function
  • platform module and platform.system().lower() function
  • if else loop
  • print function
  • variable declaration in python
  • # command line argument
  • = assignment operator
  • == comparison operator
# "Platform_Type" variable hold the platform type

#print the explame code platform type in console
print("ArunEworld: Ping test to host example")

#Need for call the os.system() with ping command
import os

#Need for identify the platform type like windows or others using platform.system().lower()
import platform 

#set the variable with host name (Enter the host here)
hostname = "www.aruneworld.com"

#This if, else loop will find the platform type
if platform.system().lower() == "windows":
    Platform_Type = "ping -n 1 "
else:
    Platform_Type = "ping -c 1 "

#print the platform type in console
print("Ping is running in ",platform.system().lower(), " Platform")

# os.system fun will return 0- got response, 1 - not got the response
if os.system(Platform_Type + hostname) == 0:
  print(hostname, "is live")
else:
  print(hostname, "is not live")

##Output
#ArunEworld: Ping test to host example
#Ping is running in  windows  Platform
#www.aruneworld.com is live

In this line Platform_Type = "ping -n 1 ", if you write without space near by value 1 like Platform_Type = "ping -n 1" then it won’t work. Because space is required after in 1 value.


Print string every one seconds

for this example you should have the knowledge of following

  • import module: Import in python is similar to #include header file in C/C++
  • time module and time.time() function.
  • while loop.
  • print function.
  • # command line argument
#Import time related functions using import function
import time

#enter into while loop
while True:

    #the time.time() function will return the current time
    print ("tick : ", time.time())
    
    #the time.sleep function will make a delay
    time.sleep(1)   #each one second
    

Example-1

Example-1
# change this code
mystring = "hello"
myfloat = 10.0
myint = 20

# testing code
if mystring == "hello":
    print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
    print("Integer: %d" % myint)

Example-2

numbers = []
strings = []
names = ["John", "Eric", "Jessica"]

# write your code here
second_name = "ArunEworld"


# this code should write out the filled arrays and the second name in the names list (Eric).
print(numbers)
print(strings)
print("The second name on the names list is %s" % second_name)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading