Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, December 1, 2021

Basic Programming in Python 3

Whitespace - Indentation

At this point, you may be wondering how Python knows which statements get grouped for if the statements while loops and function definitions and languages like C++ C and Java they use braces Python does not have any syntax for braces in MATLAB, you have the end a statement that indicates the end of a block of code in Python all of this is determined by indentation let's take a look at a couple of examples let's say X is some value in this case we see that the indentation puts the print high a statement within the code block so executing this code no matter what the value of x is will always print hello but only if X is true we'll high get printed so let's look at a slightly more the complicated example we again have a print high and then we have print hello and then aligned with this we have a print high again, in this case, these two are not aligned properly so this is maybe two spaces and this is one space here and this will cause an error in Python everything has to be aligned within a given code block now this syntax is okay and we get the same type of indentation for code blocks with functions and while loops everything is just denoted by indentation.

Read, Evaluate, Print, Loop

Hello everyone, in this article we will look at the Repple in Python. so, what is ripple stand for ripple is basically read evaluate print and then go back again to read so what does it mean it means that the shell environment in Python is an interactive environment and whatever we type into Python and when we hit enter it reads that then it evaluates whatever we have said that needs to be done and then it prints back the results to us and then it loops back to the read mode where it waits for us to give more input to Python so that it can again evaluate and print so it goes in this roundabout fashion where it reads evaluates prints and then again goes back to read so you can also go ahead and read this on Wikipedia I will leave a note for this in the in the video notes so I'll leave the hyperlink for this and if you want you can further read this let's just go and play of it with Python to understand what ripple is so let's type Python so this repple have started so this is the first print where it tells us the version and what what version of python we are using so for example if we type 5 plus 3 and hit enter python reads that evaluates it and then prints the feedback sorry prints the result said to us and then loops back to the read mode again so if i type 5 in to say 6 it has again read it evaluated at printed and looped back to the read state we can also assign variables like y is equal to 8 and that stays in the memory it has read it and then we can say why into five where it will evenly evaluate the result of fine to five and print it back to us like 40 we can also use underscore underscore basically helps his point to the last variable assignment and I can say into five it will give us four oh so 40 was the last result set and I have be used it and if I do again in to underscore into five it becomes a thousand set underscore basically refers to the last results that we've had or the last print and we can reuse that something which is an anomaly here is for example the print statement because there is nothing to evaluate if I say hello world and hit enter it just prints so in this case it reads there is nothing to evaluate but it prints and then it loops back to the read now he will just quit Python here and move back to our command prompt so I'm in Windows I will just type control Z and then hit enter and it brings me back to my Windows prompt so in this video we learned about the repel or how Python works interactively in a shell environment I hope you find this video useful and thanks for watching this video please do subscribe to my channel if you enjoy what I am doing Thanks.

Hands-on - Practice Question PYTHON 3

1. Print

Greeting Quote

Mr Greet is the name of the digital notice board placed at the entrance of the seminar hall. Its purpose is to welcome each and every participant of the seminar by showing a welcoming quote with their name on it.

It is based on the function ‘Greet’ which takes a single parameter ‘Name’ as a String are the names of the participants as Input One by one.

Write the function definition for ‘Greet’ which will generate the welcoming quote as below :

For Example, the ‘Name’ is “Ramakrishnan” then the welcoming quote will be :

Output:

Welcome, Ramakrishnan.

It is our pleasure to invite you.

Have a wonderful day.

 

Note:

Name’ must be of String Datatype.

 

Input Format for Custom Testing:

It’s a single line containing a name.

Sample Test Case 1:

 

Sample Input

STDIN      Function
-----      --------
Karthik → Name

Sample Output

Welcome Karthik.
It is our pleasure inviting you.

Have a wonderful day.

Thursday, November 25, 2021

Installing Python 3 in Windows and Linux by www.BlueTEXT.in

Introduction

Python is a widely used high-level programming language first launched in 1991. Since then, Python has been gaining popularity and is considered one of the most popular and flexible server-side programming languages.

Unlike most Linux distributions, Windows does not come with the Python programming language by default. However, you can install Python on your Windows server or local machine in just a few easy steps.

Prerequisites

  • A system running Windows 10 with admin privileges

  • Command Prompt (comes with Windows by default)

  • A Remote Desktop Connection app (use if you are installing Python on a remote Windows server)

Python 3 Installation on Windows

Step 1: Select Version of Python to Install

The installation procedure involves downloading the official Python .exe installer and running it on your system.

The version you need depends on what you want to do in Python. For example, if you are working on a project coded in Python version 2.6, you probably need that version. If you are starting a project from scratch, you have the freedom to choose.

If you are learning to code in Python, we recommend you download both the latest version of Python 2 and 3. Working with Python 2 enables you to work on older projects or test new projects for backward compatibility.

Note: If you are installing Python on a remote Windows server, log in via Remote Desktop Protocol (RDP). Once you log in, the installation procedure is the same as for a local Windows machine.

Step 2: Download Python Executable Installer

  1. Open your web browser and navigate to the Downloads for Windows section of the official Python website.

  2. Search for your desired version of Python. At the time of publishing this article, the latest Python 3 release is version 3.7.3, while the latest Python 2 release is version 2.7.16.

  3. Select a link to download either the Windows x86-64 executable installer or Windows x86 executable installer. The download is approximately 25MB.

Note: If your Windows installation is a 32-bit system, you need the Windows x86 executable installer. If your Windows is a 64-bit version, you need to download the Windows x86-64 executable installer. There is nothing to worry about if you install the “wrong” version. You can uninstall one version of Python and install another.

Step 3: Run Executable Installer

1. Run the Python Installer once downloaded. (In this example, we have downloaded Python 3.7.3.)

2. Make sure you select the Install launcher for all users and Add Python 3.7 to PATH checkboxes. The latter places the interpreter in the execution path. For older versions of Python that do not support the Add Python to Path checkbox, see Step 6.

3. Select Install Now – the recommended installation options.

For all recent versions of Python, the recommended installation options include Pip and IDLE. Older versions might not include such additional features.

4. The next dialogue will prompt you to select whether to Disable the path length limit. Choosing this option will allow Python to bypass the 260-character MAX_PATH limit. Effectively, it will enable Python to use long path names.

The Disable path length limit option will not affect any other system settings. Turning it on will resolve potential name length issues that may arise with Python projects developed in Linux.

Step 4: Verify Python Was Installed On Windows  

  1. Navigate to the directory in which Python was installed on the system. In our case, it is C:\Users\Username\AppData\Local\Programs\Python\Python37 since we have installed the latest version.

  2. Double-click python.exe.

  3. The output should be similar to what you can see below:

Note: You can also check whether the installation was successful by typing python –V in Command Prompt. The output should display your installed version of Python. In our case, it is “Python 3.7.3.”

Step 5: Verify Pip Was Installed

If you opted to install an older version of Python, it is possible that it did not come with Pip preinstalled. Pip is a powerful package management system for Python software packages. Thus, make sure that you have it installed.

We recommend using Pip for most Python packages, especially when working in virtual environments.

To verify whether Pip was installed:

  1. Open the Start menu and type "cmd."

  2. Select the Command Prompt application.

  3. Enter pip -V in the console. If Pip was installed successfully, you should see the following output:

Pip has not been installed yet if you get the following output:

'pip' is not recognized as an internal or external command,

Operable program or batch file.

If your version of Python is missing Pip, see our article How to Install Pip to Manage Python Packages on Windows.

Step 6: Add Python Path to Environment Variables (Optional)

We recommend you go through this step if your version of the Python installer does not include the Add Python to PATH checkbox or if you have not selected that option.

Setting up the Python path to system variables alleviates the need for using full paths. It instructs Windows to look through all the PATH folders for “python” and find the install folder that contains the python.exe file.

1. Open the Start menu and start the Run app.

run dialog box

2. Type sysdm.cpl and click OK. This opens the System Properties window.

3. Navigate to the Advanced tab and select Environment Variables.

4. Under System Variables, find and select the Path variable.

5. Click Edit.

6. Select the Variable value field. Add the path to the python.exe file preceded with a semicolon (;). For example, in the image below, we have added ";C:\Python34."

How to add the Variable Value durring python3 windows installation.

7. Click OK and close all windows.

By setting this up, you can execute Python scripts like this: Python script.py

Instead of this: C:/Python34/Python script.py

As you can see, it is cleaner and more manageable.

Step 7: Install virtualnv (Optional)

You have Python, and you have Pip to manage packages. Now, you need one last software package - virtualnv. Virtualnv enables you to create isolated local virtual environments for your Python projects.

Why use virtualnv?

Python software packages are installed system-wide by default. Consequently, whenever a single project-specific package is changed, it changes for all your Python projects. You would want to avoid this, and having separate virtual environments for each project is the easiest solution.

To install virtualnv:

1. Open the Start menu and type "cmd."

2. Select the Command Prompt application.

3. Type the following pip command in the console:

C:\Users\Username> pip install virtualenv

Upon completion, virtualnv is installed on your system.

Installing Python 3 on Linux

https://d33wubrfki0l68.cloudfront.net/02962eb19c0069740d16e67b5ba7c613238c8b9a/30ed2/_images/34435689480_2e6f358510_k_d.jpg

This document describes how to install Python 3.6 or 3.8 on Ubuntu Linux machines.

To see which version of Python 3 you have installed, open a command prompt and run

$ python3 --version

If you are using Ubuntu 16.10 or newer, then you can easily install Python 3.6 with the following commands:

$ sudo apt-get update
$ sudo apt-get install python3.6

If you’re using another version of Ubuntu (e.g. the latest LTS release) or you want to use a more current Python, we recommend using the deadsnakes PPA to install Python 3.8:

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.8

If you are using other Linux distribution, chances are you already have Python 3 pre-installed as well. If not, use your distribution’s package manager. For example on Fedora, you would use dnf:

$ sudo dnf install python3

Note that if the version of the python3 package is not recent enough for you, there may be ways of installing more recent versions as well, depending on you distribution. For example installing the python3.9 package on Fedora 32 to get Python 3.9. If you are a Fedora user, you might want to read about multiple Python versions available in Fedora.

Working with Python 3

At this point, you may have the system Python 2.7 available as well.

$ python

This might launch the Python 2 interpreter.

$ python3

This will always launch the Python 3 interpreter.

Setuptools & Pip

The two most crucial third-party Python packages are setup tools and pip.

Once installed, you can download, install and uninstall any compliant Python software product with a single command. It also enables you to add this network installation capability to your own Python software with very little work.

Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include pip by default.

To see if pip is installed, open a command prompt and run

$ command -v pip

To install pip, follow the official pip installation guide - this will automatically install the latest version of setup tools.

Note that on some Linux distributions including Ubuntu and Fedora the pip command is meant for Python 2, while the pip3 command is meant for Python 3.

$ command -v pip3

However, when using virtual environments (described below), you don’t need to care about that.

Pipenv & Virtual Environments

The next step is to install Pipenv, so you can install dependencies and manage virtual environments.

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.

So, onward! To the Pipenv & Virtual Environments docs!

Introduction to Python in simple Terms by www.BlueTEXT.in

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. This course is designed for software programmers who need to learn the Python programming language from scratch.

Introduction to Python

Python is a high-level, interpreted, interactive and object-oriented scripting language that finds its application in many areas like -
  • Web scripting.
  • 3d Modelling (Blender).
  • Desktop Applications -` Games (Pygame).
  • Scientific usage (SciPy/NumPy).
Python source code is available under the GNU General Public License (GPL). There are two major Python versions, Python 2 and Python 3.

Python features

  • Open Source and Simple to use.
  • Very powerful and Ubiquitous.
  • Supports broad standard library.
  • Supports interactive testing and debugging.
  • Established interface with all major DB's.
  • Runs on a variety of hardware platforms.
Technical features of Python

  • Object-oriented (supports both functional and structured programming)
  • Dynamically and strongly typed
  • Whitespace delimited (Indentation)
  • Scripting language which supports large applications.
  • High-level dynamic data types and supports dynamic type checking
  • Automatic garbage collection
  • Interpreted makes compiler interact with a developer.
  • Easy integration with CC++COMActiveXCORBA and Java.

Python Implementations

CPython - Python implementation on standard C language.
Jython - Python implementation with Java virtual machine to blend with Java.
Pypy - Python implemented in Python and its Just-in-time compiler making it fastest.
Iron Python - for windows, which implements common runtime libraries to interface with. NET.

Difference between Python2 & Python3




Comparison Parameter Python 2 Python 3
Year of Release Python 2 was released in the year 2000. Python 3 was released in the year 2008.
“Print” Keyword In Python 2, print is considered to be a statement and not a function. In Python 3, print is considered to be a function and not a statement.
Storage of Strings In Python 2, strings are stored as ASCII by default. In Python 3, strings are stored as UNICODE by default.
Division of Integers On the division of two integers, we get an integral value in Python 2. For instance, 7/2 yields 3 in Python 2. On the division of two integers, we get a floating-point value in Python 3. For instance, 7/2 yields 3.5 in Python 3.
Exceptions In Python 2, exceptions are enclosed in notations. In Python 3, exceptions are enclosed in parentheses.
Variable leakage The values of global variables do change in Python 2 if they are used inside a for-loop. The value of variables never changes in Python 3.
Iteration In Python 2, the xrange() function has been defined for iterations. In Python 3, the new Range() function was introduced to perform iterations.
Ease of Syntax Python 2 has more complicated syntax than Python 3. Python 3 has an easier syntax compared to Python 2.
Libraries A lot of libraries of Python 2 are not forward compatible. A lot of libraries are created in Python 3 to be strictly used with Python 3.
Usage in today’s times Python 2 is no longer in use since 2020. Python 3 is more popular than Python 2 and is still in use in today’s times.
Backward compatibility Python 2 codes can be ported to Python 3 with a lot of effort. Python 3 is not backwards compatible with Python 2.
Application Python 2 was mostly used to become a DevOps Engineer. It is no longer in use after 2020. Python 3 is used in a lot of fields like Software Engineering, Data Science, etc.


Print:

  • Python 2 treats “print” as a statement rather than a function.
  • Python 3 explicitly treats “print” as a function.

Integer Division:

  • Python 2 treats numbers without any digits. (Output of expression 3 / 2 is 1, not 1.5). To get the result 1.5, you would have to write 3.0 / 2.0.
  • Python 3 evaluates 3 / 2 as 1.5 by default, which is more intuitive for new programmers.

List Comprehension Loop Variables: Common name for the variables that are iterated over in a list comprehension as a global variable get interchanged. This is fixed in Python 3.

Unicode Strings: By default Python 3 stores strings as Unicode unlike Python 2.

Raising Exceptions: Python 3 requires a different syntax for raising exceptions.

  • Python 2:raise IOError, “some error message”
  • Python3: raise IOError(“some error message”)