You are here
Home > HelloWorld >

Python for Beginners

Python is a programming language.Python can be used on a server to create web applications.

Contents

“Hello, World!” Program:

print(“Hello, World!”);

output:Hello world

In this program, we have used the built-in print() function to print Hello, world! string.

Variables and Literals:

Unlike other programming languages, Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

a = 5
print(“a =”, 5)

output:a = 5

Operators:

Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

x = 20
y = 30

print(‘addition=’, x+y)

Output: addition= 18

in above program instead of + operator we also use *,,/,% etc.

Get Input from User:

In Python, you can use input() function to take input from user. For example:

inputString = input('Enter a sentence:')
print('The inputted string is:', inputString)

Python Comments:

There are 3 ways of creating comments in Python.

# This is a comment
  """This is a multiline comment.""" 
  '''This is also a  multilin

If … Else:

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

a = 33
b = 200
if b > a:
  print(“b is greater than a”)

output:b is greater than a

While Loop:

With the while loop we can execute a set of statements as long as a condition is true.

i = 1
while i < 6:
  print(i)
  i += 1

output:1 2 3 4 5 6

For Loop:

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
  print(x)

Python Data Structures:

1.Lists

A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

list1 = [“mayuri”, “soni”, “pooja”, “priya”]

print(list1[0])

ouput:mayuri

2.Tuples:

A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.

tuple= (“mayuri”,”nikita”,”manasi”)
print(ltuple[0])

output:mayuri

3.Sets:

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

set = {1, 2, 3}
print(set)

output:1,2,3

4.Dictionaries:

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.

my_dict = {1: ‘apple’, 2: ‘ball’}

Python range():

range() returns an immutable sequence of numbers between the given start integer to the stop integer.

numbers = range(1, 6)

print(list(numbers)) # Output: [1, 2, 3, 4, 5]
print(tuple(numbers)) # Output: (1, 2, 3, 4, 5)
print(set(numbers)) # Output: {1, 2, 3, 4, 5}

Classes and Objects:

Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a “blueprint” for creating objects.

The __init__() Function:

All classes have a function called __init__(), which is always executed when the class is being initiated.

The self parameter is a reference to the class itself, and is used to access variables that belongs to the class.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person(“John”, 36)

print(p1.name)
print(p1.age)

output:john 36

Functions:

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

def my_function():
  print(“Hello from a function”)

output:Hello from a fuction

my_function()

File Handling:

File handling is an important part of any web application.

Python has several functions for creating, reading, updating, and deleting files.

a.txt=

hello mayuri

f = open(“a.txt”, “r”)
print(f.read())

f = open(“a.txt”, “a”)

f.write(“new line added”)

output:a.txt=hello mayuri new line added

Exception Handling:

a = [5, 6, 7]
try:
print “Fourth element = %d” %(a[3])

except IndexError:
print(“error”)

output:error

Leave a Reply

Top