How does Python use variables, expressions, conditions and functions?

An introduction to Python has been given in this article. Now, let's start learning python.

Run your first code in Python

Python programs are not compiled but interpreted. Now, let's start writing and running python code. Please make sure python is installed on the system you are using. If not installed, download it from

Here we will use python 2.7.

Make a Python file:

Python files are stored with the extension ".py". Open a text editor and save a file named "hello.py". Open it and write the following code:

print "Hello World"
# Notice that NO semi-colon is to be used

Read file content:

Linux systems – use the "cd" command to move to the directory from the terminal where the created file (hello.py) is stored, and type the following in the terminal:

python hello.py

Windows systems – Open a command prompt and use the 'cd' command to move to the directory where the file is stored and run the file by writing the file name into the command.

Variables in Python

Variables do not need to be declared in python first. They can be used directly. Like most other programming languages, variables in python are case sensitive.

example:

a = 3
A = 4
print a
print A

The output is:

3
4

 

Expressions in Python

Arithmetic operations in python can be performed using arithmetic operators and certain built-in functions.

a = 2
b = 3
c = a + b
print c
d = a * b
print d

The output is:

5
6

 

Conditions in Python

Conditional output in python can be obtained by using if-else and elif (else if) statements.

a = 3
b = 9
if b % a = = 0 :
     print "b is divisible by a"
elif b + 1 = = 10 :
     print "Increment in b produces 10"
else :
     print "You are in else statement"

The output is:

b is divisible by a

 

Functions in Python

Functions in python are declared with the keyword "def" before the function name. The function's return type does not need to be explicitly specified in python. A function can be called by writing the function name and argument list in parentheses.

# Function for checking the divisibility
# Notice the indentation after function declaration
# and if and else statements
def checkDivisibility(a, b):
     if a % b = = 0 :
         print "a is divisible by b"
     else :
         print "a is not divisible by b"
#Driver program to test the above function
checkDivisibility( 4 , 2 )

The output is:

a is divisible by b

So, python is a very simplified and less cumbersome language to use for coding. This simplicity of python itself promotes its widespread use.

  • Next articlePython Data Types
  • Quiz – Functions in Python

Please write a comment if you find anything incorrect or would like to share more information on the above topics.

First of all, your interview preparation enhances your data structure concepts through: Python DS course.

For more information about Python development, please refer to: lsbin - IT development technology : https://www.lsbin.com/

Check out more Python-related content below:

Guess you like

Origin blog.csdn.net/u014240783/article/details/115363005