INTRODUCTION

Python is one of the most preferred programming languages in the world. Created by Guido van Rossum and was released in 1991, it is a high level, interactive, and object-oriented programming language. In everyone's mind, there is a question "why do we learn python?". In the context of that question, I would like to tell you that Python is must for students and professionals to become a great software engineer. 

Some key features of python are:-

1. Python is Interpreted

2. Python is Interactive

3. Cross-platform Language

4. Free and Open Source

5. Object-Oriented Language

6. Expressive Language

7. 7.Easy to Learn

Getting Started With Python:-

In this topic, we will run our first Python program, hello_world.py. First, you will need to check whether a recent version of Python is installed on your computer,  if it isn’t, you’ll install it. You’ll also install a text editor to work with your Python programs. Text editors recognize Python code and highlight sections as you write, making it easy to understand your code’s structure.

Environment Setup

Python differs on different systems so we need to keep some considerations in our mind. In this section, we will make sure Python is set up correctly on your system.

Installing Python:-

1. Python on windows

Windows don’t always come with Python, so you’ll probably need to install it, and then install Sublime Text.

Go to https://python.org/ and hover over the Downloads link. You should see a button for downloading the latest version of Python. After you have downloaded the file, run the python installer. Make sure you add python to the path which will make it easier to configure your system correctly.

 

Make sure you select the checkbox labeled with Add python to path











Running Python in a Terminal Session:-

Open a command window and enter python in lowercase. You should see a Python prompt (>>>), which means Windows has found the version of Python you just installed.

C:\> python

Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit

(AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>>

Now you are ready with your system to explore the programming world.

Enter the following line in your Python session, and make sure you see the output Hello Python interpreter!

>>> print("Hello Python interpreter!")

Hello Python interpreter!

>>>

Installing Sublime Text

You can download an installer for Sublime Text at https://sublimetext.com/. Click the download link and look for a Windows installer. After downloading the installer, run the installer and accept all of its defaults. Using a variable will add a little more work to the interpreter.



2. Python on macOS

You can find a Python installer for your system at https://python.org/. Hover over the Download link, and you should see a button for downloading the latest Python version. Click the button, which should automatically start downloading the correct installer for your system. After the file downloads, run the installer.

After installing enter the following command:-

$ python3 --version

Python 3.7.2

You should see output similar to this, in which case, you’re ready to try out Python.


VARIABLES AND SIMPLE DATA TYPES

In this section, you’ll learn about the different kinds of data you can work with in your Python programs.

Let’s try using a variable in hello_world.py. Add a new line at the beginning of the file, and modify the second line:

hello_world.py

mssg = "Hello Python world!"

print(mssg)

Run this program to see what happens. You should see the same output you saw previously:

Hello, Python world!


We’ve added a variable named mssg. Every variable is connected to a value, which is the information associated with that variable. In this case, the value is the "Hello Python world!" text.

Rules for naming variables:-

Variables can only contain letters, numbers, and underscore. They can start with a letter or underscore but starting with a number is not allowed. For instance, you can call a variable message_1 but not 1_message.

Spaces are not allowed in python, in spite of this underscore is used to separate the words.

Do not use words that Python has reserved for a particular programmatic purpose, such as the word print.

Be careful when using the lowercase letter l and the uppercase letter O because they could be confused with the numbers 1 and 0.

Data Types:-

Data types are variables that you use to reserve some space in memory. Variables do not need a clear cut declaration to reserve memory spaces. The declaration happens automatically when you assign a value to a variable.

STRINGS :-

A string is a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings. Strings are immutable sequence data type, i.e each time one makes any changes to a string, a completely new string object is created.a_str = 'Hello World' 

print(a_str)    #output will be whole string. Hello World

print(a_str[0])    #output will be first character. H 

print(a_str[0:5])    #output will be first five characters. Hello

NUMBERS DATA TYPE:-

Numbers have four types in Python. Int, float, complex, and long.

int_num = 10    #int value 

float_num = 10.2    #float value

complex_num = 3.14j    #complex 

value long_num = 1234567L    #long value

LIST DATA TYPE :-

A list is a collection of items in a particular order. You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family.

bicycles = ['trek', 'cannondale', 'redline', 'specialized']

print(bicycles[1])

print(bicycles[3])

This code returns the second and fourth bicycles in the list:

Output –

cannondale

specialized

Dictionary Data Type :-

Dictionary consists of key-value pairs. It is enclosed by curly braces {} and values can be assigned and accessed using square brackets[].

dic={'name':'red','age':10}

print(dic)    #will output all the key-value pairs. {'name':'red','age':10} 

print(dic['name'])    #will output only value with 'name' key. 'red' 

print(dic.values())    #will output list of values in dic. ['red',10]

print(dic.keys())    #will output list of keys. ['name','age']

TUPLE DATA TYPE :-

A tuple looks just like a list except you use parentheses instead of square brackets. Once you define a tuple, you can access individual elements by using each item’s index, just as you would for a list.

tuple = (123,'hello') 

tuple1 = ('world') print(tuple)    #will output whole tuple. (123,'hello') 

print(tuple[0])    #will output first value. (123) 

print(tuple + tuple1)    #will output (123,'hello','world') 

tuple[1]='update'    #this will give you error.







Post a Comment

Previous Post Next Post