PythonPlaza - Python & AI

Python

IntroductionVariablesIf-else
Strings Functions while-loop
For-LoopListSet
DictionaryTuple Try..Except
Class/ObjectInheritancePolymorphism
File Handling

Python - Introduction

Python is a highly popular high-level programming language created by Guido van Rossum in the late 1980s. Python places strong emphasis on code readability and simplicity, making it possible for programmers to develop applications rapidly. Like all other high level programming languages, Python code resembles the English language which computers are unable to understand. The python code we write is interpreted by a special program called the Python interpreter, which we have to install before we can code, test and execute our Python programs. There are various third-party tools like py2exe, Pyinstaller which allow us to package the Python code into stand-alone executables for Windows and Mac OS. This makes it easy for us to write Python programs that do not require the installation of Python interpreter.

Advantages of learning Python

There are various of high level programming languages like C, C++, Java that are also widely used. All these languages are very similar and vary slightly in syntax. All these languages have their own libraries. If you learn one language, you can very easily learn another programming language. If you are learning programming and new to it, Python may be first language you should try to learn. Let's list some advantages of Python.

1) It is very simple and easy to learn. Most programs in Python require only few lines of code.
2) Python has very robust libraries that can handle Collections, Strings and statistical computations.
3) Python is dynamic typed language unlike C++ or Java. You do not have to declare the variables as String, int, float or boolean, which makes the code lot easy to write.
4) Since Python is open source, you can use it free of cost. No license is required to use python.
5) You can also use Python for developing the REST webservices that can invoked from other applications.
6) Python is widely used for Machine Learning and the artificial intelligence.
7) Python also has a MVC Framework called Django which is very popular and makes backend web development very easy.
8) Python is also Object-Oriented like C++ and Java. All the applications developed in C++ and Java and also be developed in Python with ease.
9) Python is widely used for gaming applications, desktop and mobile applications.
10) Python is a cross platform language, which means the code written in Python in one operating system can be run on another operating system without making any changes.

Python Print Statement

Sometimes it is necessary to break the while loop based on certain conditions. That's where the break is used. With the break statement, you can stop the while loop even if the while condition is true. Let's see an example below.



Code Example 1:

x=25;
str="John bought a new car.";
#Print:
print(x)
print(str)
print("What a beautiful weather.")
print("I am going to school.")

#Output:
25
John bought a new car.
What a beautiful weather
I am going to school.

f-string Formatting in Python

f-string statement consists of characters f or F and a string that needs to be printed. Any variables can be passed within {}.



Code Example 2:

x=25;
city="Phoenix";

#Print:
print(f"The value of x is: {x}")
print(f"The value of city is: {city}")

#Output:
The value of x is: 25
The value of city is: Phoenix


Comments in Python code

Developers use comments to explain the Python code. Comments make the code more readable. It's a good practice to write comments in the Python code. Comments start with a #. Triple quotes (''' or """) can be used to define multiline strings and docstrings. They can also be used to "comment out" or temporarily disable large blocks of code.



Code Example 3:
#This is a comment.
print("I live Python programming")

Code Example4:
''' This will
comments large
blicks of code
'''
print("Hello World!")


Taking input in Python

In Python programs, we may need to collect information from users. It can be done using the input() function. The input function pauses the program and waits for the user to enter the input value. It returns the entered value as a string. If you prefer the input value in int or float, then you must convert it using typecasting



Code Example 5:
#show how to use input function
 varInput="input("Enter cost of a pencil")
 varPrice=float(varInput)

#Print:
 print(f"Price of a Pencil:{varPrice}")

#Output:
Enter cost of a pencil:2.30
Price of a Pencil:2.30