0% found this document useful (0 votes)
4 views6 pages

Python Programming Language-Handouts 2

The document provides a comprehensive introduction to Python programming, including how to find and install an interpreter, write and save a simple program, and understand variables and constants. It includes examples of basic Python syntax, variable assignment, and user input, as well as coding exercises for practice. The document serves as a beginner's guide to starting with Python programming.

Uploaded by

alara12malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Python Programming Language-Handouts 2

The document provides a comprehensive introduction to Python programming, including how to find and install an interpreter, write and save a simple program, and understand variables and constants. It includes examples of basic Python syntax, variable assignment, and user input, as well as coding exercises for practice. The document serves as a beginner's guide to starting with Python programming.

Uploaded by

alara12malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Programming Language

Beginning with Python programming:

1) Finding an Interpreter:
Before we start Python programming, we need to have an interpreter to interpret and
run our programs. There are certain online interpreters
like https://ide.geeksforgeeks.org/ or https://www.online-python.com/ or
https://www.w3schools.com/python/python_compiler.asp or http://ideone.com/ or
http://codepad.org/ that can be used to run Python programs without installing an
interpreter.

Windows: There are many interpreters available freely to run Python scripts like IDLE
(Integrated Development Environment) that comes bundled with the Python software
downloaded from https://www.python.org/downloads/ or
https://www.jetbrains.com/pycharm/download/#section=windows
You can download the latest version Python 3 from the above link.

macOS: You can install Python 3.13 from https://www.python.org/downloads/


Steps to be followed and remembered:
Step 1: Select Version of Python to Install.
Step 2: Download Python Executable Installer.
Step 3: Run Executable Installer.
Step 4: Verify Python Was Installed On Windows.

Your first Python Program


Save your Program

Go ahead and save your program to a file called hello.py. This is typically done
using the editor's File > Save or similar.
Run your Program
Here's how to run the program:

1. Run the file. How to do this will depend on your editor and platform.
o In IDLE, try Run > Run Module (shortcut F5).
EXAMPLES
Creating a Comment
Comments starts with a #, and Python will ignore them:

2) Example: 2
#This is a comment
print("Hello, World!")

3) Example :3
If you want to print the name of five countries, you can write:

print("USA")
print("Canada")
print("Germany")
print("France")
print("Japan")

4) Example: 4
Get String input from user
In this program we will see how to receive NAME input from user in Python.
name = input('What is your name?\n')
print ("My name is: ", name )

5) Example: 5
To enter name, age, subject etc.
name = input(“What is your name?”)
subject = input(“What is your favourite subject?”)
age = int(input(“What is your age?”))
print("My name is: ", name )
print("My subject is: ", subject )
print("My age is: ", age)
Python Variables
A variable is a named location used to store data in the memory. It is helpful to think
of variables as a container that holds data that can be changed later in the program.
For example,

number = 10
Here, we have created a variable named number . We have assigned the value 10 to
the variable.
Rules for creating variables in Python:
 A variable name must start with a letter or the underscore character.
 A variable name cannot start with a number.
 A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ ).
 Variable names are case-sensitive (name, Name and NAME are three
different variables).
 The reserved words(keywords) cannot be used naming the variable.
Assigning values to Variables in Python

As you can see from the above example, you can use the assignment operator = to
assign a value to a variable.
Example 1: Declaring and assigning value to a variable
website = "apple.com"
print(website)

Output
apple.com

In the above program, we assigned a value apple.com to the variable website . Then,
we printed out the value assigned to website i.e. apple.com

Example 2: Changing the value of a variable

website = "apple.com"
print(website)

# assigning a new value to website


website = "programiz.com"

print(website)
Output

apple.com
programiz.com

In the above program, we have assigned apple.com to the website variable initially.
Then, the value is changed to programiz.com .

Example 3: Assigning multiple values to multiple variables

a, b, c = 5, 3.2, "Hello"

print (a)
print (b)
print (c)

If we want to assign the same value to multiple variables at once, we can do this as:

x = y = z = "same"
print (x)
print (y)
print (z)

The second program assigns the same string to all the three variables x , y and z .
Constants
A constant is a type of variable whose value cannot be changed. It is helpful to think
of constants as containers that hold information which cannot be changed later.You
can think of constants as a bag to store some books which cannot be replaced once
placed inside the bag.

Assigning value to constant in Python

Example 1: Declaring and assigning value to a constant

PI = 3.14
GRAVITY = 9.8

print(PI)
print(GRAVITY)

Output

3.14
9.8
More Codes for learning
1)Let’s see the simple variable creation:

# An integer assignment
age = 45

# A floating point
salary = 1456.8

# A string
name = "John"

print(age)
print(salary)
print(name)

RESULT
45
1456.8
John
2) Variable type in Python:
# numberic
var = 123
print("Numbric data : ", var)

# Sequence Type
String1 = 'Welcome to the Python-DS World'
print("String with the use of Single Quotes: ")
print(String1)

3) Code to ask from the user two numbers and then find a Sum
and Average of it. Display the results as well.
num1 = int(input('Enter first number'))
num2 = int(input('Enter second number'))
total = num1+num2
average = total/2
print('The sum is:',total)
print('The average is:',average)

4) Write a Python program which accepts the radius of a circle


from the user and compute the area.
from math import pi
r = float (input ("Input the radius of the circle : "))
area = pi * r**2
print ("The area of the circle with radius " + str(r) + " is: " + str(area))
5) Write a Python program which accepts the length and breadth of a
rectangle from the user and calculate the area.
length = int(input ("Input the length of the rectangle: "))
breadth = int(input ("Input the breadth of the rectangle: "))
area = length * breadth
print ("The area of the rectangle is : ", area)

6) Input two number and output which number is greater.

7) Write a program to find the square of any number. [hint: take a


number from the user and put ** for a square].

a = int(input('Enter the number'))


square = a**2
print("the answer is:",square)

Coding time
Now it’s time for you to write your own codes.

1. Write a program that ask from the user his/her name and age and the print it.
2. Write a code in Python that ask from the user three numbers and then find a
sum of it. Print your sum as well.
3. Write a program to find the cube of any number. [hint: take a number from the
user and put ** for a cube].
4. Write a Python program which accepts the base and height of a parallelogram
from the user and calculate the area and perimeter and print the results.
5. Input any number and output either the number is negative or positive. [hint:
take one variable and use if-elif statement and compare from zero]
6. Input any number and output either the number is even or odd. [hint: Use if else
command and use % for finding remainder]

You might also like