🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Python Programming Language AI-102 COURSE LECT # 1 1 Overview Python is one of most popular language. Python is a high-level, versatile programming language known for its simple and readable syntax, making it an excellent choice for beginners as well as experts....

Python Programming Language AI-102 COURSE LECT # 1 1 Overview Python is one of most popular language. Python is a high-level, versatile programming language known for its simple and readable syntax, making it an excellent choice for beginners as well as experts. 2 Features of Python § Simple Syntax, Easy to Code § High-Level Language § Open Source & Free § Cross-Platform Compatibility § Multi-Paradigm § Wide Range of Applications 3 Features of Python … § Dynamic Typing § Extensive Standard Library § Rapid Development § Strong Community Support § Interpreted Language § Case Sensitive 4 Basic Data Types Python has Dynamic Typing property, So no need to tell data type. Ø Integer (int) : Whole numbers, either positive, negative, or zero. Example: x = 15 , y = -3 Ø Floating Point Numbers (float): Decimal numbers, either positive, negative, or zero. Example: x = 3.14, y = -0.5 5 Basic Data Types … Ø Strings (str): Sequences of characters, enclosed in quotes (either single quotes ' ' or double quotes " "). Example: name = “Ali” , city = ‘Multan’ Ø Boolean (bool): True or False Example: x = True, y = False 6 Expression Expressions are combinations of values, variables, and operators that the Python interpreter can evaluate to produce another value. An expression is a piece of code that returns a value. Example: ◦ 2+5 arithmetic operation, evaluates to 7 ◦ len(‘hello’) a function call expression, evaluates to 5 7 Statement Statements are instructions that the Python interpreter executes. A statement performs an action, such as creating a variable, displaying a value, or controlling the flow of execution. Example: x = 10 # Assignment operator ◦ print(“Welcome”) # Output statement 8 Arithmetic Operators Ø Addition + Ø Subtraction – Ø Multiplication * Ø Division / (gives result in float) 5/2 gives 2.5 Ø Floor Division // (gives result in integer) 17//5 gives 3 Ø Exponent **, 2 ** 3 gives 8 Ø Modulus %, 17 % 5 gives 2 9 Input Ø input() function is used to take input from users. By default input() returns input as string: name = input("Enter your name: ") Ø Converting Input to Other Data Types If you need a different data type, you must explicitly convert it. num = int ( input("Enter a number: ") ) Or num = input("Enter a number: ") num = int(num) 10 String Operations § Concatenation: Joining two or more strings together using the + operator. str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result) # Output: "Hello World" § Repetition: Repeating a string multiple times using the * operator. str1 = "Hello" result = str1 * 3 print(result) # Output: "HelloHelloHello" 11 String Operations …2 § Slicing: Extracting a portion of a string using square brackets [] and indices. str1 = "Hello, World!" print(str1[0:5]) # Output: "Hello" print(str1[7:12]) # Output: "World" print(str1[:5]) # Output: "Hello" (from start to index 4) print(str1[7:]) # Output: "World!" (from index 7 to end) § Length: Getting the number of characters in a string using the len() function. str1 = "Hello, World!" print(len(str1)) # Output: 13 12 String Operations …3 § Case Conversion: Changing the case of characters in a string. str1 = "Hello, World!" print(str1.upper()) # Output: "HELLO, WORLD!" print(str1.lower()) # Output: "hello, world!" print(str1.capitalize()) # Output: "Hello, world!" print(str1.title()) # Output: "Hello, World!" print(str1.swapcase()) # Output: "hELLO, wORLD!" § Splitting: Splitting a string into sub strings str1 = "Hello, World!" words = str1.split() # Default split by whitespace print(words) # Output: ["Hello,", "World!"] str2 = "one,two,three" parts = str2.split(",") # Split by comma print(parts) # Output: ["one", "two", "three"] 13 String Operations …4 § Replacing: Replace a sub string with another sub string str1 = "Hello, World!" print(str1.replace("World", "Python")) # Output: "Hello, Python!" 14 Q&A 15

Use Quizgecko on...
Browser
Browser