Learn Python Basics

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. High-level data structures: Python comes with builtin data structures like lists, dictionaries and tuples that make it easy to store and manipulate data.
  6. 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.
  7. 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.
  8. 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.

 

 

if you run this code in your favorite IDE you will receive the output of Hello World like below

Python Hello World

 

 

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.

 

you can do this with different data types like string integer of float below is the example.

 

 

This is the output

Python Variables

 

 

 

Python Operators

Like every programming languages, in python also you can do different arithmetic operations below is the example

 

 

This is the result

Python Operators

 

 

 

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.

 

 

This is the result

Python Input User

 

 

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 [ ].

 

 

This is the result

Python List

 

 

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 ( ).

 

 

 

This is the result

Python Tuples

 

 

 

Sets: sets are Python another collection, sets are unordered collection and the every elements in sets should be unique.

 

This is the result

Python Sets

 

 

 

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

Python Dictionary

 

 

Control Flow Statements

If Else Statement: the general syntax for Python if statement.

 

 

This is simple example 

 

 

In this case, if the condition (x) evaluates to True, the suite (the print() function call) will be executed.

 

 

This is the result

Python If Else

 

 

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.

 

 

This is the result

Python While Loop

 

 

 

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 For Loop

 

 

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

 

 

This is the result

Python Functions

 

 

You can add parameters to functions

 

 

This is the result

Python Parametr function

 

 

 

Python function that returns something

 

 

This is the result

Python Multiplication

 

 

 

Python Recursion Functions

a recursion means that a function calls it self

 

 

This is the result

Python Recursive Function

 

 

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.

 

 

This is the result

Python Lambda Function

 

 

 

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

 

 

Reading from a file

 

 

Reading the specified characters of file

 

 

Writing to a file

 

 

 

 

Also you can watch the complete video for Learn Python Basics

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×