Python – Tuples

Python Tuples can be thought of as Read-Only Lists. Tuple can store different datatype elements. A Tuple contains items separated by commas and enclosed within square parentheses ( ( ) )Tuple is a collection which is ordered and unchangeable. Allows duplicate members.

Features of Python Tuples

  1. Ordered Lists
  2. Cannot change values after declaring elements
  3. Allow duplicate members
  4. Uses Parentheses ( )
  5. Can store different datatypes

Declaring Tuple in Python

tuple1 = ("Study", 123, 1.25, "Gyaan", 456)
tuple2 = ('Huzaif', 789)

Note – The plus (+) sign is the concatenation operator and the asterisk (*) is the repetition operator. For example −

tuple1 = ("Study", 123, 1.25, "Gyaan", 456)
tuple2 = ('Huzaif', 789)

print(tuple1)
print(tuple1[1])
print(tuple1[0:3])
print(tuple1[2:])
print(tuple2*2)
print(tuple1+tuple2)
Filed under: Python

Python – Lists

Python Lists are very similar to Array. List can store different datatype elements. A list contains items separated by commas and enclosed within square brackets ([ ]). List is a collection which is ordered and changeable. Allows duplicate members.

Features of Python Lists

  1. Ordered Lists
  2. Can change vlaues after declaring elements
  3. Allow duplicate members
  4. Uses Brackets [ ]
  5. Can store different datatypes

Declaring List in Python

list1 = ['Study', 123, 1.25, 'Gyaan', 456]
list2 = ['Huzaif', 789]
print(list1)
print(list2)

Note – The plus (+) sign is the concatenation operator and the asterisk (*) is the repetition operator. For example −

list1 = ['Study', 123, 1.25, 'Gyaan', 456]
list2 = ['Huzaif', 789]

print(list1)
print(list1[0])
print(list1[0:3])
print(list1[2:])
print(list2*2)
print(list1+list2)
Filed under: Python

Python – String

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows either pair of single( ‘ Single Quote String  ‘) or double quotes( ” Double Quote String “). 

Declaring String in Python

x = 'Single'
y = "Double"
print(x,y)

Note – The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example −

x = 'Study Gyaan'

print(x)
print(x[0])
print(x[0:5])
print(x[6:])
print(x*2)
print(x+'.com')
Filed under: Python

Python – Variables

Python Variable Data Types

Variables are nothing but reserved memory for storing data. Based on the data type interpreter allocates the memory. Python Variable are  similar to JavaScript Variables. Unlike Python is Loosely-Coupled which means we can declare a variable without defining it’s data type.

Python have different data types:

  1. Number : Integer, Float, Complex
  2. String
  3. List
  4. Tuple
  5. Dictionary

Assigning Values to Variables

x = 3        # Float
y = "StudyGyaan"    # String
z = 2.15 # Float print(x) print(y)
print(z)

Variables do not need to be declared with any particular data type and can even change type after they have been declared and stored.

x = 5
x = "StudyGyaan"
print(x)

Multiple Assignment 

In python variable values can be assign multiple time

x = y = z = 786

Rules for Initialising Variables or Identifiers in Python

A python identifiers starts with A to Z or a to z or an underscore( _ ) followed by zero or more letters, underscores and digits (0 to 9).

Allowed Identifiers: A, a, _a, _A123, _123, etc.

Not Allowed Identifiers: 123, $ads, etc.

Output Variables

Python variables are basically output using print statement. We can use + symbol to concate two strings and add two number variables in Python.

x = "awesome"        # String
print("Python is " + x)

# Output: Python is awesome

x = 5.5           # Float
y = 10            # Integer
print(x + y)

# Output: 15.5

If you try to combine string and number, it will throw error:

x = "Beginners Guide"
y = 10
print(x + y)

#Output: TypeError: unsupported operand type(s)
# for +: 'int' and 'str'
Filed under: Python

Python – Basic Syntax

Running Python programs is very similar like java, C, programs. Python can be run in different modes

Interpreter or Shell mode

For Starting python shell in command line type bellow command on Windows:

C:\python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

On Linux or Ubuntu:

$ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Python File mode

You can create file with extension .py and run it in the terminal:

$ python3 file.py
Hello, World!

Print Hello World in  Python

Once you start python shell in windows or ubuntu type bellow program in cmd.

print("Hello World!!!")

Rules for Initialising Variables or Identifiers in Python

A python identifiers starts with A to Z or a to z or an underscore( _ ) followed by zero or more letters, underscores and digits (0 to 9).

Allowed Identifiers: A, a, _a, _A123, _123, etc.

Not Allowed Identifiers: 123, $ads, etc.

Comments in Python

For single line comment in Python # symbol and “”” for multiple line is used:

# single line comment
"""
Multiple Line Comment
"""

Line and Indentation

In python every Line is consider as new block of code and python does not uses braces instead Indentation which means Indentation is very important

if 5>2:
print('Five greater than 2')
Filed under: Python