Professional Documents
Culture Documents
PHP Unit-1
PHP Unit-1
PHP 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:
Example:
print("Hello, World!").
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.
name) print(type(name))
Output:
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:
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.
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
function.
Examples:
4
4. COMMANDS AND VARIABLES:
Variables:
C reating Variables :
Example x = 5
y = "John"
print(x) print(y)
Python Comments:
Creating a Comment:
5
Example:
Creating a Comment
Example
#This is a comment
print("Hello, World!").
5. READING INPUT FROM THE KEYBORAD: User Input:
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:
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…
greeting()
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()”
Statements…
When the function is called, then you need to specify a value for the arguments:
functionName(ValueforArg1, ValueforArg2)
Example:
Output:
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( ):
language( )
Output:
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
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