Core Python lesson-1 | From scratch to loops

by ahin subhra das

Python is a dynamic, high-level, and interpreted programming language. It supports Object Oriented programming approach to develop applications. It is simple and easy to learn and provides lots of high-level data structures.

History of Python :

Python was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of Python programming language has taken from the ABC programming language.

There is also a fact behind the choosing name Python. Guido van Rossum was a fan of the popular BBC comedy show of that time, “Monty Python’s Flying Circus”. So he decided to pick the name Python for his newly created programming language.

Why we learn Python?

:

  • Interpreted Language
  • Object-Oriented Language
  • Open Source Language
  • Extensible
  • Learn Standard Library
  • GUI Programming Support
  • Integrated
  • Embeddable
  • Dynamic Memory Allocation
  • Wide Range of Libraries and Frameworks

Python is Platform-Independent or dependent ?

: Python is a binary platform-independent programming language. The same Python code can run on virtually all operating systems and platforms. However, some precautions must be taken when programming with Python, such as minding case-sensitivity and avoiding certain modules, in order to avoid compatibility issues.

Python First Program:

print(“hello , python world! ”)

Basic Programs :

a=int(input(“enter 1st number: ”))

b=int(input(“enter 2nd number: ”))

print(a+b)

##note: Here, if user input contains digits only, int() function parses integer otherwise a ValueError is encountered.

The int() function converts the specified value into an integer number.

name=input(“enter your name:”)

print(“your name is ”,name)

Python Comments:

Comments are generally used to explain the code. We can easily understand the code if it has a proper explanation.

To apply the comment in the code we use the hash(#) at the beginning of the statement or code.

# This is the print statement

print(“Hello Python”)

Python If-else statements :

The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.

1.Program to check whether a person is eligible to vote or not:

age = int (input(“Enter your age? “))

if age>=18:

print(“You are eligible to vote !!”);

else:

print(“Sorry! you have to wait !!”);

2: Program to check whether a number is even or not:

num = int(input(“enter the number?”))

if num%2 == 0:

print(“Number is even…”)

else:

print(“Number is odd…”)

elif statement:

number = int(input(“Enter the number?”))

if number==10:

print(“number is equals to 10”)

elif number==50:

print(“number is equal to 50”);

elif number==100:

print(“number is equal to 100”);

else:

print(“number is not equal to 10, 50 or 100”);

a = int(input(“Enter a? “));

b = int(input(“Enter b? “));

c = int(input(“Enter c? “));

if a>b and a>c:

print(“a is largest”);

if b>a and b>c:

print(“b is largest”);

if c>a and c>b:

print(“c is largest”);

Loops :

Python for loop:

#for loop syntax:

for iterating_var in sequence:

statement(s)

#for loop flow-chart :

n = 4

for i in range(0, n):

print(i)

here, range( start, stop, step size)

2.Iterating string using for loop

str = “Python”

for i in str:

print(i)

Nested for loop:

# User input for number of rows

rows = int(input(“Enter the rows:”))

# Outer loop will print number of rows

for i in range(0,rows+1):

# Inner loop will print number of Astrisk

for j in range(i):

print(“*”,end = ‘’)

print()

rows = int(input(“Enter the rows”))

for i in range(0,rows+1):

for j in range(i):

print(i,end = ‘’)

print()

Using else statement with for loop:

for i in range(0,1,5):

print(i)

else:

print(“for loop completely exhausted.”)

While loop in python :

#while loop syntax:

while expression:

statements

#while loop flow-chart:

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

Note: remember to increment i, or else the loop will continue forever.

2: Program to print table of given numbers:

i=1

number=0

b=9

number = int(input(“Enter the number:”))

while i<=10:

print(“%d X %d = %d \n”%(number,i,number*i))

i = i+1

i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1

str = “python”

for i in str:

if i == ‘o’:

break

print(i);

Continue statement in python :

continue flow-diagram:

i = 0

while(i < 10):

i = i+1

if(i == 5):

continue

print(i)

str = “python”

for i in str:

if(i == ‘T’):

continue

print(i)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store