You are on page 1of 11

UNIT - 1.

1
1. INTODUCTION
Python is a popular programming language. It was
created by Guido van Rossum, and released in 1991.

It is used for:

• web development (server-side),


• software development,  mathematics,
• system scripting.

• Python can be used on a server to create web applications.


• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
• Python can be used to handle big data and perform complex mathematics.
• Python can be used for rapid prototyping, or for production-ready software
development.

Example:

print("Hello, World!").

2. INPUT PROCESSING AND OUTPUT:

How to Take Input from User in Python:

Sometimes a developer might want to take user input at some point in the program.
To do this Python provides an input() function.

2
Syntax: input('prompt')

where prompt is an optional string that is displayed on the string at the time of
taking input.

Example 1: Python get user input with a message

# Taking input from the user name =

input("Enter your name: ")

# Output print("Hello, " +

name) print(type(name))

Output:

Enter your name: GFG


Hello, GFG <class
'str'>
How to Display Output in Python:

Python provides the print() function to display output to the standard output
devices. Syntax:
Print (value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush) Example: Python Print Output:

# Python program to demonstrate

# print() method print("GFG")

# code for disabling the softspace feature

print('G', 'F', 'G')

3
Output:

GFGG F G

In the above example, we can see that in the case of the 2nd print statement there is
a space between every letter and the print statement always add a new line character
at the end of the string.

3. DISPLAYING OUTPUT IN PRINT ()FUCTION:

Print () function in Python:


We can use print () function in Python to display some message or output on

Python shell. The Syntax of print function is:

Here EXPRESSION is the variable of any data type or string value that we want to
display.

In previous versions of Python there was no need of parenthesis with print() function

but in Python version 3 , it is mandatory to use pair of parenthesis with print()

function.

Examples:

>>>print (“Welcome to Python”) # It is acceptable in Python version 3 print()

automatically inserts a new line character at the end.

4
4. COMMANDS AND VARIABLES:

Variables:

Variables are containers for storing data values.

C reating Variables :

Python has no command for declaring a variable.

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

Example x = 5
y = "John"
print(x) print(y)

Python Comments:

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Creating a Comment:

Comments starts with a #, and Python will ignore them:

5
Example:

#This is a comment print("Hello, World!") Python Comments

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Creating a Comment

Comments starts with a #, and Python will ignore them:

Example

#This is a comment
print("Hello, World!").
5. READING INPUT FROM THE KEYBORAD: User Input:

Python allows for user input.

That means we are able to ask the user for input.

The method is a bit different in Python 3.6 than Python 2.7.

Python 3.6 uses the input() method.

Python 2.7 uses the raw_input() method.

The following example asks for the username, and when you entered the username,
it gets printed on the screen:

Python 3.6
username = input("Enter username:") print("Username is: " +
username).

Example:

username = raw_input("Enter username:") print("Username is: " + username).

6
Functions:
In Python, a function is a group of related statements that performs a specific task.
Functions help break our program into smaller and modular chunks. As our program
grows larger and larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes the code reusable.

Syntax:
def function_name(parameters):
Statements…

Statements…

Rules to define a function:


1. Keyword def that marks the start of the function header.
2. A colon (:) to mark the end of the function header.
3. One or more valid python statements that make up the function body.
Statements must have the same indentation level (usually 4 spaces).

Basic Example of Function:


Following the basic syntax above, an example of a basic Python function printing
“Hello World” to the terminal looks like this:

greeting()

 def is the built-in keyword in Python that is used to declare function.


 greeting is the name of the function.
 After the name we should give open “(“ and close “)” parenthesis where we
can give parameters.
 : indicates the start of the function body.

7
 Print(“Hello Students”) is the code in the function body followed by the
indentation.
 To call this function, write the name of the function followed by parentheses
as we write above “greeting()”

The output of the above program will be Hello Students.

Arguments in Python Functions


While defining a function in Python, we can pass argument(s) into the function by
putting them inside the parenthesis.

The basic syntax for doing this looks as shown below:

def functionName(arg1, arg2):

Statements…

When the function is called, then you need to specify a value for the arguments:

functionName(ValueforArg1, ValueforArg2)

Example:

Output:

Sum of Given number is 30

 First we have declared a function called “addOfTwo”. And giving two


parameters.
 Then we are adding the two parameters and storing the result in sum.
 After that we are print the sum.

8
 After the function we are calling the function addofTwo(10, 20) with two
arguments. This two value will be assigned to num1 and num2 and give the
result.

Global Variables
In Python, a variable declared outside of the function or in global scope is known as
a global variable. This means that a global variable can be accessed inside or outside
of the function.

Example:

name = “Python”

def language( ):

print(name, “- Inside of Function”)

language( )

print(name, “- Outside of Function”)

Output:

Python - Inside of Function

Python - Outside of Function

In the above code, we have created name as a global variable and defined a function
called language() to print the global variable to print name. Finally we call the
language() which will print the value of name.

Local Variables
Variable that are declared inside a function is called local variable. These variables
can’t be accessed outside of the function. For example

def student():

name = “Zenitsu”

print(name)

9
student()

Output:

Zenitsu

Here we have declared name variable inside a function called student, this name
variable can be accessed only inside of the function. if we try to access the name
variable outside the function it will Throw a NameError that ‘name’ is not
defined.

Global Constant
A variable can be declared on the module level and use it in the module as
a global variable. And we can also import it to other modules.

Mymodule.py

Greeting = 'Vanakam'

def cfunc():

global Greeting

print(Greeting)

cfunc()

nextModule.py

from Mymodule.py import Greeting

if we run nextModule.py the output will be:

Vanakam

In the Mymodule.py program we have declared a variable called Greeting and assign
it to Vanakam then inside the function and we’re adding global keyword before the
variable name then we are calling that function.

10
Then we are creating a new python file called nextModule.py inside that file we are
importing Greeting from Mymodule.py and print’s Vanakam

11

You might also like