Skip to content

Instantly share code, notes, and snippets.

@amelieykw
Last active July 12, 2018 18:33
Show Gist options
  • Select an option

  • Save amelieykw/69646d9cac5262f5366a9a9f43b1bfa5 to your computer and use it in GitHub Desktop.

Select an option

Save amelieykw/69646d9cac5262f5366a9a9f43b1bfa5 to your computer and use it in GitHub Desktop.
[Python - Variables] #Variables #Python #GeeksforGeeks
  1. Variables, Expressions & Functions
  2. Maximum possible value of an integer
  3. Transpose a matrix in Single line
  4. Global and Local Variables
  5. Partial Functions
  6. Packing and Unpacking Arguments
  7. end parameter
  8. Type Conversion
  9. Byte objects vs Strings

Variables

Python programs are not compiled, rather they are interpreted.

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.

a = 3
A = 4
print a
print A

The output is :

3
4

Expressions

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

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

The output is :

5
6

Conditions

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

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.

# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment