How to write a program to accepts a string from console and print the characters that have even indexes?

Example:

If the following string is given as input to the program:
H1e2l3l4o5
Then, the output of the program should be:
Hello

print("Please enter text to print::")
inputchars = input()

if inputchars:
	string = ""
	for i in inputchars:
		if inputchars.index(i)%2 == 0:
			string += str(i)
	
	print('-------------------')
	print("You Entered:", inputchars)
	print("Result:")
	print(string)
# OUTPUT::
Please enter text to print::
H1e2l3l4o5
-------------------
You Entered: H1e2l3l4o5
Result:
Hello

How to find the odd and even numbers using Python?

Here, In below code, user can enter two numbers to find the odd and even numbers between them. The number must be in between 1000 and 3000.

print("Please Enter the Number between 1000 and 3000")

print("Enter the First Number:")
first_number = input()

print("Enter the Second Number:")
second_number = input()

print("First Number: ", first_number)
print("Second Number: ", second_number)


if int(first_number) >= 1000 and int(second_number) <= 3000:
	first_number = int(first_number)
	second_number = int(second_number)

	even_numbers= []
	odd_numbers = []

	for i in range(first_number,(second_number+1)):
		if i%2 is 0:
			even_numbers.append(str(i))
		else: 
			odd_numbers.append(str(i))
	
else:
	print("Please enter a number between 1000 and 3000")

How to read or write files in Python?

Before any files to read we have to know how to Open & Close the files.

# Open Function:

we can open a file using python build in function open()

file_object = open(file_name, file_access_mode )
  • file_name = It is the file name or the full file path.
  • file_access_mode = File access mode is to determine, open file for read, write , append etc.
ModesDescription
rOpens a file for read only purpose.
rbOpen a file for reading only in binary format.
r+Open a file for both operation reading & writing.
rb+Open a file for both reading & writing in binary format.
wOpen a file for writing purpose only.
It overwrites the existing file or otherwise create a new file.
wbOpens a file for writing only in binary format.
It overwrites the existing file or otherwise create a new file.
aOpens a file for appending.
abOpens a file for appending in binary format.
ab+Opens a file for both appending & reading in binary format.
w+Opens a file for both reading & writing format.
wb+Opens a file for both reading & writing in binary format.

# Write Function:


file_content = "This is my First Text."
file_object.write( file_content ) 

# Read Function:

file_object.read()

# Rename Files:

os.rename( old_file_name, new_file_name )

# Delete File:

os.remove( file_name )

# Close File:


file_object.close()

# File Object Attributes:

  • file_object.closed
  • file_object.mode
  • file_object.softspace [ It is no longer supported in python 3.x ]
  • file_object.name

How to Install Virtualenv in Ubantu 16.04 or Windows?

Ubantu or Linux

When we are going to run any project in Python Django in the Windows or Ubantu system, we need the virtual environment installed into our system. So first we will see how to install virtual environment into our system.

How to install virtual
virtualenv or environment:

For Ubantu or Linux System:

sudo apt-get install python3-pip

In Windows System, we can use this commands..

apt-get install python3-pip

OR

apt-get install pip

Then install virtualenv using pip3 or pip

For Ubantu System we can use…

sudo pip3 install virtualenv 

OR

sudo pip install virtualenv 

For Windows System, we can use..

pip install virtualenv 


Now create a virtual environment

virtualenv venv 

We can also use a Python interpreter of your choice. like Python 3, Python 3.5 etc

For Ubantu , Linux or Windows we can use this command

virtualenv -p /usr/bin/python2.7 venv

Create virtualenv using Python3

virtualenv -p python3 myenv

Active your virtual environment:

For Ubantu or Linux System..

source venv/bin/activate

Activate Virtual Environment Using fish shell.:

source venv/bin/activate.fish

Activate Virtual Environment using Windows..

Here, the path at venv root path or same directory.

venv/Scripts/activate