1 / 5

More Basics of Python

More Basics of Python. Common types of data we will work with String ( str ): a list of characters in order, surrounded by a pair of single or double quotes. E.g., ‘Hello’, ‘1234’, “How are you?” Integers ( int ): a whole number. E.g., 1, -5, 34567

jcastro
Download Presentation

More Basics of Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Basics of Python • Common types of data we will work with • String (str): a list of characters in order, surrounded by a pair of single or double quotes. E.g., ‘Hello’, ‘1234’, “How are you?” • Integers (int): a whole number. E.g., 1, -5, 34567 • Decimals (float): numbers with decimals. E.g. 1.0, 2.3, -3.4

  2. Operations for Python Data # concatenation x = ‘Hello’ y = ‘world!’ z = x + ‘ ‘ + y print(z) # hello world! # slicing x = ‘Hello world!’ y = x[0:5] z = x[6:] print(y) # Hello print(z) # world! Strings # len() x = ‘Hello world!’ print(len(x)) # 12 # arithmetic x = 2.0 y = 3.0 print(x + y) # 5.0 print(x - y) # -1.0 print(x * y) # 6.0 print(x // y) # 0.0 print(x / y) # 0.6666 print(x ** y) # 8.0 # arithmetic x = 2 y = 3 print(x + y) # 5 print(x - y) # -1 print(x * y) # 6 print(x // y) # 0 print(x / y) # 0.6666 print(x ** y) # 8 Integers Floats

  3. Functions • We’ll study functions more extensively. • Here are some of the basics of a function Key word to designate this is a function Function inputs defcompute( num1, num2 ): print(‘We saw ‘ + str(num1) + str(num2)) if num1 > num2: result = 2*num1 + num2 else: result = 2*num2 + num1 return result Function name Function result

  4. Use of Functions • A Python function is a block of code that does something • Similar to a math function • x = add(2, 3) • y = compute(2, 3) • answer = input(‘Enter a value : ‘) # answer a string • n = int(input(‘Enter a value : ‘)) # n now an int

  5. Use of Python Built-in Functions • Python has a huge collection of built-in functions we can use. These functions are stored in various library files. • To use Python functions in a library, we import the library first, then use any functions in that library. import random print(‘Some random values ‘, random.randint(3, 10))

More Related