Home | | Problem Solving and Python Programming | Values and types - Python

Chapter: Problem Solving and Python Programming : Data, Expressions, Statements

Values and types - Python

A value is one of the basic things a program.

Values and types

A value is one of the basic things a program. There are different values integers, float and strings. The numbers with a decimal point belong to a type called float. The values written in quotes will be considered as string, even it’s an integer. If type of value is not known it can be interpreted as

Eg:

>>> type('Hello, World!')

<type 'str'>

>>> type(17)

<type 'int'>

>>>        type('17')

<type 'str'>

>>>        type('3.2')

<type 'str'>

 

Variables  

A variable is a name that refers to a value. A variable is a location in memory used to store some data (value). They are given unique names to differentiate between different memory locations. The rules for writing a variable name are same as the rules for writing identifiers in Python. The assignment operator (=) to assign values to a variable. An assignment statement creates new variables and gives them values:

Eg:

>>> message = 'And now for something completely different'

>>> n = 17

>>> pi = 3.1415926535897932

The type of a variable is the type of the value it refers to.

Eg:

>>> type(message)

<type 'str'>

>>> type(n)

<type 'int'>

>>> type(pi)

<type 'float'>

 

Variable names and keywords

Variable names can be arbitrarily long and contain both letters and numbers, but they have to begin with a letter. The underscore character, _, can appear in a name. It is often used in names with multiple words, such as my_name or airspeed_of_unladen_swallow. If you give a variable an illegal name, you get a syntax error:

Eg:

>>>        76trombones = 'big parade'

>>>        SyntaxError: invalid syntax

>>>        more@ = 1000000 SyntaxError: invalid syntax

class = 'Advanced Theoretical Zymurgy'

SyntaxError: invalid syntax

The interpreter uses keywords to recognize the structure of the program, and they cannot be us d as variable names. Python 2 has 31 keywords. In Python 3, has 33 keywords.


 

Python Identifiers

 Identifier is the name given to entiti s like class, functions, variables etc. in Python. It helps differentiating one entity from anoth r.

 

Rules for writing identifiers

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_1 and print_this_to_screen, all are valid example.

2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.

3. Keywords cannot be used as identifiers.

4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.

5. Identifier can be of any length.

 

Data types in Python

In Python programming, data types are actually classes and variables are instance (object) of these classes. They are defined as int, float and complex class in Python.

 

Lists

List is an ordered sequence of items. Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list

 of comma-separated values (items) between square brackets. List items need not all have the same type.

Eg:

>>>        a = [’spam’, ’eggs’, 100, 1234]

>>>        a

Output: [’spam’, ’eggs’, 100, 1234]

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Data, Expressions, Statements : Values and types - Python |

Related Topics

Problem Solving and Python Programming : Data, Expressions, Statements


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.