0% found this document useful (0 votes)
12 views2 pages

Python Basics: Data Types & Functions

The document provides a series of Python code snippets demonstrating various programming concepts including data types, conditional statements, built-in methods for lists, tuples, and dictionaries, mathematical functions, and control structures. It covers practical examples such as checking for positive or negative numbers, calculating factorials, checking leap years, and handling exceptions. Additionally, it illustrates the use of functions, recursion, and default arguments.

Uploaded by

vhgshreyas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Python Basics: Data Types & Functions

The document provides a series of Python code snippets demonstrating various programming concepts including data types, conditional statements, built-in methods for lists, tuples, and dictionaries, mathematical functions, and control structures. It covers practical examples such as checking for positive or negative numbers, calculating factorials, checking leap years, and handling exceptions. Additionally, it illustrates the use of functions, recursion, and default arguments.

Uploaded by

vhgshreyas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Demonstrate different data types and conditional statements


x = 10 y = 2.5 z = "Python" w = True print(type(x)) print(type(y)) print(type(z)) print(type(w)) num =
int(input("Enter a number: ")) if num > 0: print("Positive") elif num < 0: print("Negative") else:
print("Zero")

2. Built-in methods of List, Tuple and Dictionary


lst = [1, 2, 3] [Link](4) print(lst) tpl = (10, 20, 30) print([Link](20)) d = {"a": 1, "b": 2} d["c"] = 3
print(d)

3. Mathematical functions using math module


import math print([Link](16)) print([Link](5)) print([Link](2, 3))

4. Check Positive, Negative or Zero


num = int(input("Enter number: ")) if num > 0: print("Positive") elif num < 0: print("Negative") else:
print("Zero")

5. Grade based on marks


marks = int(input("Enter marks: ")) if marks >= 90: print("Grade A") elif marks >= 75: print("Grade
B") elif marks >= 60: print("Grade C") else: print("Fail")

6. Leap year check


year = int(input("Enter year: ")) if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print("Leap Year") else: print("Not Leap Year")

7. Factorial using for loop


n = int(input("Enter number: ")) fact = 1 for i in range(1, n + 1): fact = fact * i print("Factorial =", fact)

8. Sum of digits using while loop


n = int(input("Enter number: ")) sum = 0 while n > 0: sum = sum + n % 10 n = n // 10 print("Sum of
digits =", sum)

9. Largest of three numbers


a = int(input("Enter a: ")) b = int(input("Enter b: ")) c = int(input("Enter c: ")) if a > b and a > c: print(a)
elif b > c: print(b) else: print(c)

10. Even numbers in a list


lst = [1, 2, 3, 4, 5, 6] for i in lst: if i % 2 == 0: print(i)
11. Prime number check
n = int(input("Enter number: ")) if n <= 1: print("Not Prime") else: for i in range(2, n): if n % i == 0:
print("Not Prime") break else: print("Prime")

12. Area of rectangle using function


def area(l, b): return l * b print(area(10, 5))

13. Default arguments


def add(a=10, b=20): print(a + b) add() add(5, 6)

14. Local and global variables


x = 10 def show(): x = 5 print(x) show() print(x)

15. Factorial using recursion


def fact(n): if n == 1: return 1 return n * fact(n - 1) print(fact(5))

16. Fibonacci using recursion


def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2) for i in range(5): print(fib(i))

17. Exception handling division


try: a = int(input()) b = int(input()) print(a / b) except: print("Error")

18. Reciprocal using try-except-finally


try: n = int(input()) print(1 / n) except: print("Error") finally: print("Done")

You might also like