Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Getting Started With Python Programming (Part 1)

The document provides an introduction to Python programming, covering basic concepts such as print statements, data types, and user input. It explains how to perform arithmetic operations, use loops, and handle strings, including counting characters and reversing case. Additionally, it discusses the importance of comments and variable naming in code for better understanding.

Uploaded by

Lakshya Siwatch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Getting Started With Python Programming (Part 1)

The document provides an introduction to Python programming, covering basic concepts such as print statements, data types, and user input. It explains how to perform arithmetic operations, use loops, and handle strings, including counting characters and reversing case. Additionally, it discusses the importance of comments and variable naming in code for better understanding.

Uploaded by

Lakshya Siwatch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

GETTING STARTED WITH PYTHON PROGRAMMING

The code on the left hand side shows different print


statements in Python.
Anything enclosed in single (‘) or double (“) quotation
marks is displayed on the screen as it is.
If we use a non-graphic character (\t) with a print
statement then, it will be printed after one tab space.

If you observe the code given on the left hand side


carefully, you will notice that the two strings “Good” and
“Morning” are given in two separate lines hence, the
output has also come in two lines.
But, when we have used the keyword, end=’ ‘, the print
statement given on the next line has also printed with the
previous line.

In the code given on the left hand side, there are 3 variables.

The first statement is asking the user to enter a number and


this input is stored in variable a.

The second statement is asking the user to enter another


number and this input is stored in variable b.

In the third statement, variable c is storing the value of sum of


the two variables a and b.

In the fourth statement, the print statement is displaying the


result of the sum of a and b.
a is the Holding Variable
= is the assignment operator. (RHS identity will be passed to LHS)
int means that the data type of the input will be taken as integer.
c = a+b The value of a and b will be added and transferred to c
print(“Sum is:”,c) This statement will display the result which is
stored in c. The string “Sum is:” is given in
double quotation marks hence, this will be
displayed as it is.

This code is similar to the code given above but, there is an


important thing to notice that int is missing from the input
statements. This means, whatever input will be given by the
user, it will be taken as a string.

Strings can’t be added but, they are only concatenated


(joined).

Since, the values 10 and 5 are treated as strings hence, the


result is displayed as 105 in spite of 15 which was displayed
in the previous example.
USING DATA TYPES IN INPUT AND OUTPUT

In the program code given on the left hand


side, two variables (a and b) of integers type
are initialized. These variables are getting
values from the user as input.

There is a third variable c, which is holding


the value of the quotient coming after
dividing a by b.

You will notice that the result is coming in


floating point numbers whereas, a/b is used
to give the quotient and it must be in whole
number.

In the program code given on the left hand side, two


variables (a and b) of integers type are initialized.
These variables are getting values from the user as
input.

There is a third variable c, which is holding the value


of the quotient coming after dividing a by b.

Unlike the above program code, you must have


noticed that the result is coming in whole number.
This is because, of the syntax
c = int(a/b) where, the result of a/b is made to come
as integer.

It is quite noticeable from the code of Left hand side that


when two variables are taken as input in float (fraction)
type, there result after addition can be printed as integer by
writing int with the result.

Otherwise, it would have generated the output in floating


point only.
PROGRAM TO ILLUSTRATE AREA & PERIMETER CALCULATION

In the program code given on the LHS,


the user is asked to enter the side of a
square. That value as input is stored in
the variable ‘side’.

Two variables ‘area’ and ‘perimeter’ are


used to store the values of area and
perimeter of the square respectively.

Finally, the values of Area and Perimeter


are printed.

Note: For the sake of understanding,


name of variables are taken as ‘side’,
‘area’ and ‘perimeter’. You can have
any other names for these variables.

One more thing to note in the above program code is the heading of the program, which is enclosed in triple
quotes (‘’’). These statements which are enclosed in triple quotes are called comments. When the program
is run, these statements are ignored by the compiler/interpreter. These statements are basically given for
the ease of programmer.
WORKING WITH ITERATIVE STATEMENTS (loop)
Iterative statements means writing statements under iteration (loop). This means that the statement which
is/are given under iteration are repeated as per the given values in the range.
There are 3 components of a range:
Start: The value from where loop initializes.
Stop: The value upto which the loop iterates. It is always 1 step less than the given value.
Step: The value which defines the number of jumps taken by the loop.
There are 2 types of loops in Python, for loop and while loop.

In the code given on the LHS, for loop is incorporated with loop
variable as i and range from 5 to 16.

As per the explanation given above the start value is 5, stop value is 16
which will terminate at 15 (one step prior) and the step value is 1.

Please note that a colon (:) sign is given after the for statement and
with this the next statement (print) is coming as indented.

The loop variable (i) is given in the print statement with end=”,”. By this
the output will come in a single horizontal line separated with commas.

This example is same as the previous one. The only difference is that,
here start value and stop values are given but step value is missing.

Please note,
In case, the step value is not given, it is taken as 1 by default.

This example is also same as the previous two examples. The only
difference is that, here the step value is given as 3. So, the output will
come from 5 skipping 2 values in each iteration.

Please note,
In this example, the loop is terminated at 14. Skipping 2 steps.

This example is also same as the previous two examples. The only
difference is that, here the step value is given as 3. So, the output will
come from 5 skipping 2 values in each iteration.

Please note,
In this example, the loop is terminated at 14. Skipping 2 steps.
Program to ask the user enter any string and display the number of UPPERCASE letters, LOWERCASE letters,
DIGITS and SPECIAL characters.

Explanation of the above program code:


In this program, s is a variable in which a string is stored which is entered by the user. up, low, dig and spl
are another variables to hold the values of Uppercase letters, Lowercase letters, Digits and Special
Characters respectively. These variables are initialized with 0.
One more variable ‘l’ is taken for holding the length of the string. Function len(name of string) is used to
count the number of characters in the string.
Iteration (loop) is incorporated in which each letter of the string is checked for Uppercase, Lowercase,
Digits and Special Characters with isupper(), islower() and isdigit() functions respectively.
The variables up, low, dig and spl are updated by 1 as soon as the ‘if’ condition is satisfied.
Else condition will take care for the Special characters.
Finally, the data is displayed by printing the variables u

Program to ask the user enter any string and display the string with all the letters in their reverse case,
using built-in functions. (Uppercase letters to lowercase and vice-versa.)

Explanation of the above program:


In this program, a variable ‘s’ is taken to store user’s input for a string. for loop is taken for traversing the
characters of the string from beginning to end.
Conditional statement ‘if’ is given to check whether the character i is uppercase or lowercase. After
satisfying the ‘if’ condition, the value of ‘i' is changed accordingly and printed.
Else statement is taken to handle the characters which are not letters, they will be printed as it is.

You might also like