0% found this document useful (0 votes)
2 views6 pages

Python Session 3

The document explains constants and variables in Python, highlighting that constants are immutable and should be named in uppercase. It also covers different types of literals, including numeric, string, boolean, and special literals, as well as collections like lists, tuples, dictionaries, and sets. Additionally, it provides examples of how to declare and use these elements in Python code.

Uploaded by

rajasekhar reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python Session 3

The document explains constants and variables in Python, highlighting that constants are immutable and should be named in uppercase. It also covers different types of literals, including numeric, string, boolean, and special literals, as well as collections like lists, tuples, dictionaries, and sets. Additionally, it provides examples of how to declare and use these elements in Python code.

Uploaded by

rajasekhar reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Constants

A constant is a type of variable whose value cannot be changed.

Declaring and assigning value to a constant

Create a [Link]:

PI = 3.14
GRAVITY = 9.8

Create a [Link]:

import constant

print([Link])
print([Link])

Output

3.14
9.8

Rules and Naming Convention for Variables and constant


 Constant and variable names should have a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example:

snake_case

MACRO_CASE

camelCase

CapWords

 Create a name that makes sense. For example, vowel makes more sense than v .
 If you want to create a variable name having two words, use underscore to separate them. For
example:
my_name

current_salary

 Use capital letters possible to declare a constant. For example:

PI

MASS

SPEED_OF_LIGHT

TEMP

 Never use special symbols like !, @, #, $, %, etc.


 Don't start a variable name with a digit.

Literals

Literal is a raw data given in a variable or constant. In Python, there are


various types of literals they are as follows:

Numeric Literals

Numeric Literals are immutable (unchangeable). Numeric literals can belong


to 3 different numerical types: Integer , Float , and Complex .

How to use Numeric literals in Python?

a = 0b1010 #Binary Literals


b = 100 #Decimal Literal
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal

#Float Literal
float_1 = 10.5
float_2 = 1.5e2

#Complex Literal
x = 3.14j

print(a, b, c, d)
print(float_1, float_2)
print(x, [Link], [Link])

Output

10 100 200 300


10.5 150.0
3.14j 3.14 0.0

String literals

A string literal is a sequence of characters surrounded by quotes. We can use


both single, double, or triple quotes for a string. And, a character literal is a
single character surrounded by single or double quotes.

strings = "This is Python"


char = "C"
multiline_str = """This is a multiline string with more than one line
code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"

print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)

Output
This is Python
C
This is a multiline string with more than one line code.
Ünicöde
raw \n string

Boolean literals

A Boolean literal can have any of the two values: True or False .

How to use boolean literals in Python?

x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10

print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)

Output

x is True
y is False
a: 5
b: 10

Special literals

Python contains one special literal i.e. None . We use it to specify that the field
has not been created.

How to use special literals in Python?


drink = "Available"
food = None

def menu(x):
if x == drink:
print(drink)
else:
print(food)

menu(drink)
menu(food)

Output

Available
None

Literal Collections

There are four different literal collections List literals, Tuple literals, Dict
literals, and Set literals.

How to use literals collections in Python?

fruits = ["apple", "mango", "orange"] #list


numbers = (1, 2, 3) #tuple
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
vowels = {'a', 'e', 'i' , 'o', 'u'} #set

print(fruits)
print(numbers)
print(alphabets)
print(vowels)

Output

['apple', 'mango', 'orange']


(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}

You might also like