Skip to main content

Python Basics Literals and Variables

Literals

Python Variables


Python can do any athematic calculations but to bring it to a meaningful representation, reusability and easy referencing a value we use variable.

Valid variable names


A variable name can be combination of letters, numbers, and underscore. The variable name must start with either letter or underscore symbol. No other symbol or numbers allowed in the first character.

Example variable names:

  
server_name
total_servers
cpu_load_average
server10_mem_size
  
The Variable name defined in lower case is different that defined with upper case, they are created at different memory locations.

  
cpu_load_average not same as CPU_load_average
mem not same as MEM
  

Invalid Variable definations


We should not use any type of operator symbols in defining the variable. Here is some variables defined throws errors
  
c@paci+y ~ not valid @ and + 
total_@f_server ~ using @ not valid
10thserver ~ initial number not allowed  
Web Server load ~ spaces are not allowed instead we can use underscores
  
Note that variable name should not be use that is a datatype in Python example : int, float cannot be used.

In Python Variables defined in the following way:
  • The variables allow you to store values
  • A variable has a valid name (letters, digits, underscore, not a reserved keyword)
  • Python is dynamically typed that means variables can be redeclared/reassigned
  • We can use shortcut operators in order to cleanly redeclare a variable 
  • We can combine text and variables using the '+' operator in the print() function
print("Product cost:"+ product_cost)


The print() function


Comments in Python

As per my observation and learnings
  • Comments allows other developers in the team understand your code
  • A comment is created by a #followed by text
  • Do not write unnecessary comments in your python program write self document code that will be better
  • A multiline comment should have a hash in front of every line

The input() function

This input function is going  to prompt the user to get the data from the console.

The input function accepts an optional parameter that can be used in order to write message before the user inputs.

The input() method returns string value. So, if we want to perform arithmetic operations, we need to cast the value first.


How the print function is printing output, just opposite to that input() function reads the data in python.

Age =input("how old are you?")

In Python3, whatever you entered as input, the input() built-in function converts it into a string.

Comments

Popular posts from this blog

Python Sets and Dictionaries

Python Sets Python Maps examples A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. Dictionaries consist of pairs (called items) of keys and their corresponding values. Dictionaries can be created by placing a comma-separated list of key: value pairs within curly braces Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Python Set Operations and methods Python Set methods continueed >>> SET={'new','old','list','new'} >>> SET set(['new', 'old', 'list']) Lets play with set operations, it is more like school math here... >>> s={1,2,3,4} >>> k={3,4,5,6} >>> len(s) 4 >>> len(k) 4 >>> 1 in s True >>...

Python Interview Questions -Coding Snipets

 This post is dedicated for all DevOps Engineer, Software Engineers who are preparing for Coding Interviews,  Overview of Coding Interviews Most Companies looking for People with minimum Coding knowledge. In a coding interview, you will be given a small problem to solve within 10 - 20 minutes online screen or in-person on their system. In the question, you might be having some part of the code framed and you might be asked to write a snippet of code in between. You need to understand the code comments and proceed to build the expected snippet of code. Bigger companies look for the General purpose questions, where small companies look for specific questions. The General questions would be like this: Determine if the given word is a palindrome or not. (Example madam) Determine given number is prime or not. How to prepare for a coding interview? Now we have the flexibility to choose the programming language on which you are comfortable.  In general DevOps Infra guys will be ...

Files IO & Pickles

How to make this FILE process in Python?  The Standard Input, Standard Output are the two fileobjects genrally accessible from the operating system provided interfaces which allows us to change files. How to redirect this output... normally output is coming from print command in the Python. Can you send the data/text to a file? Yes we can using file object. File IO Process in Python The file open modes Python file open modes are same as C language by default it will be open in read mode. The open() method returns a file object or file pointing reference.  Syntax:  fileobject = open(file_name [, access_mode][, buffering])  where basic file operational mode can be 'r' (read), 'w'(write) or 'a'(append).  The default mode is 'r' that is read mode and the file can be ANSI Text file or binary file (b). They can be opened in combined mode ‘rb+’  That every file that you opened in the code must be closed. The close() method closes an ope...