Basics of python language:
To write a c++ program, the following
details are needed.
1. Command-line:
- It describes what's going on inside a program, so that person looking at the source code does not have a hard time figuring it out.
Types of comments:
(i) Single
line comments:
Syntax:
#Statements
Example:
#print function
(ii)
Multiline comments:
Syntax:
‘’’Set Statements ‘’’
- A variable is nothing but it allocates the memory space and holds the values.
Syntax fro declaring variables:
variable_name=value;
Example:
a=0
#int data type
s=’
‘ #String data type
Rules for naming the variables:
- The first character must be an alphabet or an underscore (_).
- No commas or blank spaces are allowed within a variable name.
- No special symbols, an underscore can be used in a variable name.
3. Data types in python:
- The most widely used data types in the input function is:
4. Output function:
- This function is used to show details on the output screen for user understanding.
Syntax:
print(“Statements”) or
print(‘Statements’)
5.
Escape sequence:
- This is used to align output on the screen.
Escape sequence
| Description | Example | Output |
\n | Newline or next line | print(“Hai! \n I am Sabrina.”) | Hai! I am Sabrina. |
\t | Horizontal tab(one tab space) | print(“Hai\t Hello”) | Hai hello |
\’ | Display single-quotes | print(“I don\”t know”) | I don’t know |
\” | Display double-quotes | print(“/“Hey/”’) | “Hey” |
\\ | Print slash | print(“a//b”) | a/b |
- This function is used to get value from users.
Syntax:
Variable_name
= data_type(input(“ Sentence “))
Example:
a=int (input(“Enter a value : “))
Example
program: save example.py
Explanation:
name=input("Enter
your name : ")
or
name=str(input("Enter
your name : ")) both are same for getting string values.
Output:
Example
2:
- If need multiple inputs at one line, then refer to the below program.
Program:
Explanation:
- split() function is used to separate a string from each space.
- int(a)+int(b) means, a and b value has string so should be changed into number data type because here performing add operation.
Output:
Example 3:
- If displaying value at the sentence like “Add of a and b value is : 10” as “Add of 5 and 5 value is :10”
Program:
Explanation:
- In print function, the + operator is used to join variable of value to a string value.
Output:
No comments:
Post a Comment