Open In App

Python | Set 2 (Variables, Expressions, Conditions and Functions)

Last Updated : 14 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Introduction to Python has been dealt with in this article. Now, let us begin with learning python.

Running your First Code in Python 
Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on the system you are working on. If it is not installed, download it from here. We will be using python 2.7.

Making a Python file: 
Python files are stored with the extension “.py”. Open a text editor and save a file with the name “hello.py”. Open it and write the following code:

Python3




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


Time Complexity: O(1)
Auxiliary Space: O(1)

Reading the file contents: 
Linux System – Move to the directory from the terminal where the created file (hello.py) is stored by using the ‘cd’ command and then type the following in the terminal : 

python hello.py

Windows system – Open command prompt and move to the directory where the file is stored by using the ‘cd’ command and then run the file by writing the file name as a command.

Variables in Python 
Variables need not be declared first in python. They can be used directly. Variables in python are case-sensitive as most of the other programming languages. 

Example: 

Python3




a = 3
A = 4
print (a)
print (A)


The output is : 

3
4

Time Complexity: O(1)

Auxiliary Space: O(1)

Expressions in Python 
Arithmetic operations in python can be performed by using arithmetic operators and some of the in-built functions. 

Python3




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


The output is : 

5
6

Time Complexity: O(1)

Auxiliary Space: O(1)

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

Python3




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

Time Complexity: O(1)

Auxiliary Space: O(1)

Functions in Python 
A function in python is declared by the keyword ‘def’ before the name of the function. The return type of the function need not be specified explicitly in python. The function can be invoked by writing the function name followed by the parameter list in the brackets. 

Python3




# 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

Time Complexity: O(1)

Auxiliary Space: O(1)

So, python is a very simplified and less cumbersome language to code in. This easiness of python is itself promoting its wide use.



Previous Article
Next Article

Similar Reads

Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Regular Expressions in Python - Set 2 (Search, Match and Find All)
Regular Expression in Python with Examples | Set 1The module re provides support for regular expressions in Python. Below are main methods in this module. Searching an occurrence of pattern re.search() : This method either returns None (if the pattern doesn't match), or a re.MatchObject that contains information about the matching part of the strin
4 min read
Mathematical Functions in Python | Set 1 (Numeric Functions)
In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns t
3 min read
Overuse of lambda expressions in Python
What are lambda expressions? A lambda expression is a special syntax to create functions without names. These functions are called lambda functions. These lambda functions can have any number of arguments but only one expression along with an implicit return statement. Lambda expressions return function objects. For Example consider the lambda expr
8 min read
Extracting email addresses using regular expressions in Python
Let suppose a situation in which you have to read some specific data like phone numbers, email addresses, dates, a collection of words etc. How can you do this in a very efficient manner?The Best way to do this by Regular Expression. Let take an example in which we have to find out only email from the given input by Regular Expression. Examples: In
3 min read
Python List Comprehensions vs Generator Expressions
What is List Comprehension? It is an elegant way of defining and creating a list. List Comprehension allows us to create a list using for loop with lesser code. What normally takes 3-4 lines of code, can be compressed into just a single line. Example: # initializing the list list = [] for i in range(11): if i % 2 == 0: list.append(i) # print elemen
3 min read
Python | Generate Personalized Data from given list of expressions
Given different lists of expressions, the task is to generate a random combination of these expressions in a csv file for some purposes such as test databases contrary to ordinal data simulator which generates datasets from random numbers and standard names. Examples: Input :lists of your own expressions Names = ['David', 'Emilia', 'John', 'Karmen'
2 min read
Plot Mathematical Expressions in Python using Matplotlib
For plotting equations we will use two modules Matplotlib.pyplot and Numpy. This module helps you to organize your Python code logically. Numpy Numpy is a core library used in Python for scientific computing. This Python library supports you for a large, multidimensional array object, various derived objects like matrices and masked arrays, and ass
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg