0% found this document useful (0 votes)
5 views3 pages

Ut 1

This document covers fundamental Python concepts including variables, conditional statements, and loops. It explains the usage of if, else, elif, while, and for statements, along with break, continue, and pass statements. Additionally, it provides examples to illustrate the functionality of these constructs in Python programming.
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)
5 views3 pages

Ut 1

This document covers fundamental Python concepts including variables, conditional statements, and loops. It explains the usage of if, else, elif, while, and for statements, along with break, continue, and pass statements. Additionally, it provides examples to illustrate the functionality of these constructs in Python programming.
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
You are on page 1/ 3

Unit 1

Python concepts : origin, comparison , comments, variables and assignment, identifiers,


basic style guidelines, standard types , internal types , operators, built – in functions,
numbers and strings . sequences : strings, sequences, string – operators & functions , special
features of strings , memory management , programs & examples

Conditional and loops : if statement, else statement, Elif statement , while statement , for
statement, break statement, continue statement , pass statement , else statement

If statement is written by using the if keyword.


If statement:
a = 33
a = 33 b = 200
b = 200 if b > a:
if b > a: print("b is greater than a") # you will get an error
print("b is greater than a") cause of indentation property of python

If else Statement
The if-else statement in Python is used to execute a block of code when the condition in
the if statement is true, and another block of code when the condition is false.
age=25
print ("age: ", age)
if age >=18:
print ("eligible to vote")
else:
print ("not eligible to vote")

If elif else Statement

The if elif else statement allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE

number = 10
if number > 10:
print("The number is greater than 10.")
elif number == 10:
print("The number is exactly 10.")
else:
print("The number is less than 10.")
While Loop

A while loop in Python programming language repeatedly executes a target statement as


long as the specified boolean expression is true. This loop starts with while
keyword followed by a boolean expression and colon symbol (:). Then, an indented block of
statements starts.

count = 1

while count <= 5:


print("Count is:", count)
count += 1 # Increase count by 1 each time

For loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a
set, or a string).
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set
etc.

fruits = ["apple", "banana", "cherry"] Output :


apple
for x in fruits: banana
print(x) cherry

The break Statement

With the break statement we can stop the loop before it has looped through all the items

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
if x == "banana":
break
The continue Statement
With the continue statement we can stop the current iteration of the loop, and continue
with the next:

fruits = ["apple", "banana", "cherry"] o/p


for x in fruits: apple
if x == "banana": cherry
continue
print(x)

Else in For Loop


The else keyword in a for loop specifies a block of code to be executed when the loop is
finished:
for x in range(2,5):
o/p
print(x) 2
else: 3
print("Finally finished!") 4
Finally finished!

The pass statement is used as a placeholder for future code.


When the pass statement is executed, nothing happens, but you avoid getting an error when
empty code is not allowed.

i=1
while (i < 5):
if (i == 2):
pass # Skip when i is 2
else:
print(i)
i += 1

You might also like