Post Top Ad

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, December 28, 2018

December 28, 2018

Create virtual environments for python with conda

1. Check conda is installed and in your PATH


  1. Open a terminal client.
  2. Enter conda -V into the terminal command line and press enter.
  3. If conda is installed you should see somehting like the following.
$ conda -V
conda 3.7.0

2. Check conda is up to date

  1. In the terminal client enter
conda update conda
  1. Upadate any packages if necessary by typing y to proceed.

3. Create a virtual environment for your project

  1. In the terminal client enter the following where yourenvname is the name you want to call your environment, and replace x.x with the Python version you wish to use. (To see a list of available python versions first, type conda search "^python$" and press enter.)
conda create -n yourenvname python=x.x anaconda
  1. Press y to proceed. This will install the Python version and all the associated anaconda packaged libraries at “path_to_your_anaconda_location/anaconda/envs/yourenvname”

4. Activate your virtual environment.

  1. To activate or switch into your virtual environment, simply type the following where yourenvname is the name you gave to your environement at creation.
source activate yourenvname
  1. Activating a conda environment modifies the PATH and shell variables to point to the specific isolated Python set-up you created. The command prompt will change to indicate which conda environemnt you are currently in by prepending (yourenvname). To see a list of all your environments, use the command conda info -e.

5. Install additional Python packages to a virtual environment.

  1. To install additional packages only to your virtual environment, enter the following command where yourenvname is the name of your environemnt, and [package] is the name of the package you wish to install. Failure to specify “-n yourenvname” will install the package to the root Python installation.
conda install -n yourenvname [package]

6. Deactivate your virtual environment.

  1. To end a session in the current environment, enter the following. There is no need to specify the envname - which ever is currently active will be deactivated, and the PATH and shell variables will be returned to normal.
source deactivate

6. Delete a no longer needed virtual environment

  1. To delete a conda environment, enter the following, where yourenvname is the name of the environment you wish to delete.
conda remove -n yourenvname -all

Tuesday, December 11, 2018

December 11, 2018

The For Loop in Python

for loop is used for iterating over a sequence


Example : 

fruits = ["apple""banana""cherry"]
for in fruits:
  print(x)


Note: The for loop does not require an indexing variable to set beforehand.

Looping Through a String

for x in "banana":
  print(x)

The break Statement

Exit the loop when x is "apple":


fruits = ["orange""apple""cherry"]
for x in fruits:
  print(x) 
  if x == "apple":
    break



The continue Statement


With the continue statement we can stop the current iteration of the loop, and continue with the next:



fruits = ["orange""apple""cherry"]
for x in fruits:
  if x == "apple":
    continue
  print(x)



The range() Function

To loop through a set of code a specified number of times, we can use the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

for x in range(6):
  print(x)



Else in For Loop


The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

Print all numbers from 0 to 5, and print a message when the loop has ended:

for x in range(6):
  print(x)
else:
  print("Finally finished!")

Nested Loops

A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":


adj = ["red""big""tasty"]
fruits = ["apple""banana""cherry"]

for x in adj:
  for y in fruits:
    print(x, y)




Sunday, May 20, 2018

May 20, 2018

How to Check if a number is Odd or Even in Python 3

In this post, we will learn how to check whether a number entered by the user is even or odd.

You must have a basic knowledge of Operator and Control Statement in Python!



We also need to understand the meaning of the Odd and Even number So we can write a program easily.



A number is an even number if it can be divisible by 2 otherwise it's an odd number.


num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("{0} is Even".format(num))
else:
   print("{0} is Odd".format(num))


Please Check. If you have any problem just send me an email : admin@khemputhea.com

Sunday, October 29, 2017

October 29, 2017

Python Installation using Anaconda


Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.




Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn't catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.

Please check the video for installation step :  click here