In this Python article we are going to talk about Learn Python Basics, so first of all let’s talk about Python.
What is Python ?
Python is probably the easy and nicest programming language in widespread use. Python code is clear to read and write, and it is concise without being cryptic. Python is a very expressive language, which means that we can usually write far fewer lines of Python code than would be required for an equivalent application written in, say, C++ or Java. Python is cross platform language: In general, the same Python program can be run on Windows and Unix-like systems such as Linux, BSD, and Mac OS X, simply by copying the file or files that make up the program to the target machine, with no “building” or compiling necessary. It is possible to create Python programs that use platform-specific functionality, but this is rarely necessary since almost all of Python’s standard library and most third-party libraries are fully and transparently cross-platform.
What are Python Features ?
Python is powerful and high level programming language with many features that make it popular among developers. these are some of the key features of Python:
- Simple and easy to learn: Python is designed to be easy to read and write, with simple and consistent syntax that makes it easy to learn.
- Interpreted language: Python is an interpreted language, which means that the code is executed line by line as it is interpreted, rather than being compiled into machine code before execution. this makes development and debugging faster and easier.
- Object-oriented: Python is an object oriented language, which means that it uses objects to represent data and functionality. this makes it easy to organize and structure code and to reuse code across different projects.
- Dynamically-typed: Python is dynamically typed language, which means that the data type of a variable is determined at runtime, rather than being specified in advance. this makes it easy to write code quickly, without worrying too much about types.
- High-level data structures: Python comes with builtin data structures like lists, dictionaries and tuples that make it easy to store and manipulate data.
- Extensive standard library: Python has large and comprehensive standard library, which provides many useful modules and functions for tasks like networking, web development and data analysis.
- Cross-platform: Python runs on many different platforms, including Windows, macOS and Linux and this makes it easy to develop and deploy code across different environments.
- Open-source: Python is an open source language, which means that the source code is available for anyone to use, modify and distribute.
In this Learn Python Basics article we are going to learn different topics in Python.
Also you can check Python Object Oriented Programming
Installation For Python3
If you have a modern and up to date Mac or other Unix-like system you may already have Python 3 installed. you can check by typing python -V (note the capital V) in a console (Terminal.app on Mac OS X)—if the version is 3.x you’ve already got Python 3 and don’t have to install it yourself. If Python wasn’t install to your operating system.
For Windows and Mac OS X, it is easy-to-use graphical installer packages are provided by Python website that take you step-by-step through the installation process. this is the installer for Python Python Setup Installer
For Linux, BSD, and other Unixes (apart from Mac OS X for which a .dmg installation file is provided), the easiest way to install Python is to use your operating system’s package management system. In most cases Python is provided in several separate packages. For example, in Ubuntu (from version 8), there is python3.0 for Python, idle-python3.0 for IDLE (a simple development environment), and python3.0-doc for the documentation—as well as many other packages that provide add-ons for even more functionality than that provided by the standard library. (Naturally, the package names will start with python3.1 for the Python 3.1 versions, and so on.) If no Python 3 packages are available for your operating system you will need to download the source from www.python.org/download and build Python from scratch. Get either of the source tarballs and unpack it using tar xvfz
After installation you need and IDE (Integrated Development Environment) we are using Pycharm IDE community edition that is completely free you can download Pycharm IDE from the this link Click To Download Pycharm IDE.
Now we are going to write our first hello world application.so like every programming language python also has something called print for printing our first Hello World.
1 |
print( "Hello World ") |
if you run this code in your favorite IDE you will receive the output of Hello World like below
Python Variables
Variables are used for storing data in the memory so below is the variable of x that we re storing 20 in to that.
1 |
x = 20 |
you can do this with different data types like string integer of float below is the example.
1 2 3 4 5 6 |
x = 20 a = "Hello World" y = 14.5 print(x) print(a) print(y) |
This is the output
Python Operators
Like every programming languages, in python also you can do different arithmetic operations below is the example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
x = 30 y = 20 # Adding two operands in python print('x + y =', x+y) # Subtraction of two operands in python print('x - y =', x-y) # Multiplication of two operands in python print('x * y =', x*y) # Division of two operands in python print('x / y =', x/y) # Floor division in python print('x // y =', x//y) # Python Reminder print('x % y =', x%y) # Power operands print('x ** y =', x**y) |
This is the result
How To Get User Input In Python ?
getting User Input in Python is simple, there is a built in function input() that you can use.
1 2 |
name = input('Enter Your Name:') print('Your name is ' + name) |
This is the result
Python Collections
There are different collections in python programming language
List: Python lists can be used to hold any number of data items of any data types.Lists are
mutable, so we can easily insert items and remove items whenever we want. you can create lists by using [ ].
1 2 3 4 5 6 7 8 9 |
mylist1 = [7, 4, 2, 20, 25, 36, 24] mylist2 = ['Python', 'Java', 'C++', 'C#'] mylist3 = ['Python', 100, -20, 'aardvark', 500] mylist4 = [ ] print(mylist1) print(mylist2) print(mylist3) print(mylist4) |
This is the result
Tuples: Python tuples can be used to hold any number of data items of any data types.tuples are immutable, so once they are created we cannot change them. you can create lists by using ( ).
1 2 |
prolanguages = ("Python", "Java", "C++", "C#") print(prolanguages) |
This is the result
Sets: sets are Python another collection, sets are unordered collection and the every elements in sets should be unique.
This is the result
Dictionary: dictionary is also an unordered collection, it is key value collection, it means that for every element we have a key and value.
This is the result
Control Flow Statements
If Else Statement: the general syntax for Python if statement.
1 2 3 4 5 6 7 8 9 |
if boolean_expression1: suite1 elif boolean_expression2: suite2 ... elif boolean_expressionN: suiteN else: else_suite |
This is simple example
1 2 |
if x: print("this is x") |
In this case, if the condition (x) evaluates to True, the suite (the print() function call) will be executed.
1 2 3 4 5 6 7 8 9 10 |
x = 20 if x < 30: print("Smaller") elif x > 30: print("Greater") else: print("Smaller") |
This is the result
While Loop: while loop is used to iterate over a block of code as long as the condtion is true. this is the example for the while loop.
1 2 3 4 |
x = 1 while x < 10: print(x) x += 1 |
This is the result
For Loop: In Python programming language, for loop is used to iterate over a sequence (list, tuple, string) or other iterable objects. this is the example for for loop.
This is the result
Python Function
Functions are means by which we can package up and parameterize functionality. Four kinds of functions can be created in Python: global functions, local functions, lambda functions, and methods.
Global functions as the name suggests it is a global function that you can access it every where Local functions (also called nested functions) are functions that are defined inside other functions. These functions are visible only to the function where they are defined, they are especially useful for creating small helper functions.Lambda functions are expressions, so they can be created at their point of use, however, they are much more limited than normal functions. Methods are functions that are associated with a particular data type and can be used only in conjunction with the data type.
Example of creating functions
1 2 3 4 5 6 7 8 9 |
def Hello(): print("Hello World") Hello() Hello() Hello() Hello() |
This is the result
You can add parameters to functions
1 2 3 4 5 6 7 |
def sum(a,b): print(a+b) sum(5,6) sum(7,8) |
This is the result
Python function that returns something
1 2 3 4 5 6 7 8 |
def multiply(a): return a * 6 print(multiply(5)) |
This is the result
Python Recursion Functions
a recursion means that a function calls it self
1 2 3 4 5 6 7 8 9 |
def factorial(a): if a == 1: return 1 else: return (a * factorial(a-1)) b = 6 print("Factorial ", b, "is", factorial(b)) |
This is the result
Python Lambda Function
A lambda function is anonymous function, in python we can use lambda keyword for this. this is the syntax for Lambda Functions lambda parameters: expression.
1 2 3 4 |
area = lambda b, h: 0.5 *b*h print(area(6,5)) print(area(3,6)) |
This is the result
Python File Handling
Most programs need to save and load information, such as data or state information, to and from files. Python provides many different ways of doing this.
so in Python you can use open() in for File Handling and there are some modes that you can use .
“r” – Read – Default value. Opens a file for reading
“a” – Append – Opens a file for appending
“w” – Write – Opens a file for writing and if the file does not exist it will create that.
“x” – Create – Creates the specified file
You can do like this
1 |
file = open("myfile.txt") |
Reading from a file
1 2 |
file = open("myfile.txt", "r") print(f.read()) |
Reading the specified characters of file
1 2 |
file = open("myfile.txt", "r") print(f.read(8)) |
Writing to a file
1 2 |
file = open("myfile.txt", "a") f.write(" the new added text to file") |
Also you can watch the complete video for Learn Python Basics
Subscribe and Get Free Video Courses & Articles in your Email